How do I fix this error? It starts with 2.

It's supposed to start at one when the program is ran.

I fixed my indentation and I know I didn't use reference values correctly, but I'm not sure how to fix the fact it starts at two.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Ashton Dreiling
//Pennies for pay exercise
#include <iostream>
#include <stdlib.h>


using namespace std;

//module prototype
void whichLoop (double days);

//global constants
const double ZERO_POINT_ZERO_ONE_FOR_CALCULATIONS = 0.01;
const double ZERO_FOR_CALCULATIONS = 0;
const double ONE_FOR_CALCULATIONS = 1;
const double HUNDRED_FOR_CALCULATIONS = 100;
const double TWO_FOR_PENNY_CAL = 2;

int main()
{
    // some variables
    double days;



    //Display purpose of program and table headings
    cout << "Today we are going to calculate the amount of money you'd earn over a period of time" << endl;
    cout << "if your salary is one penny the first day and doubles from there." << endl;
    cout << "Enter a number of days" << endl;
    cin >> days;
    cout << "Day\t\tPay\t\tTotal" << endl;
    cout << "---------------------------------------" << endl;
    whichLoop (days);

    system("Pause");


    return 0;
}//end main

void whichLoop (double days)

{
    double penny = ZERO_POINT_ZERO_ONE_FOR_CALCULATIONS;
    double counter = ONE_FOR_CALCULATIONS;
    double salary;
    double total;
    double totalPay;

    while(counter <= days)
    {
        salary = (penny );
        total = total + salary;
        penny = (penny * TWO_FOR_PENNY_CAL);
        counter = (counter + ONE_FOR_CALCULATIONS);
        cout << counter << "\t\t" << salary << "\t\t" << total << endl;
        totalPay = total;
    }//end of while loop

    cout << "The total payment of your time working here will be $ " << totalPay << endl;

}//end of whichLoop module
Hi FBHSIE,
My post might be not very helpful, but I always find it weird when you declare your variables like this :

1
2
3
4
5
const double ZERO_POINT_ZERO_ONE_FOR_CALCULATIONS = 0.01;
const double ZERO_FOR_CALCULATIONS = 0;
const double ONE_FOR_CALCULATIONS = 1;
const double HUNDRED_FOR_CALCULATIONS = 100;
const double TWO_FOR_PENNY_CAL = 2;


For me, they just feel long, unmemorable and a little bit fuzzy. What are the valid reasons for you to do this?
compiling on cpp.sh here's the output

Today we are going to calculate the amount of money you'd earn over a period of time
if your salary is one penny the first day and doubles from there.
Enter a number of days
31
Day		Pay		Total
---------------------------------------
2		0.01		0.01
3		0.02		0.03
4		0.04		0.07
5		0.08		0.15
6		0.16		0.31
7		0.32		0.63
8		0.64		1.27
9		1.28		2.55
10		2.56		5.11
11		5.12		10.23
12		10.24		20.47
13		20.48		40.95
14		40.96		81.91
15		81.92		163.83
16		163.84		327.67
17		327.68		655.35
18		655.36		1310.71
19		1310.72		2621.43
20		2621.44		5242.87
21		5242.88		10485.8
22		10485.8		20971.5
23		20971.5		41943
24		41943		83886.1
25		83886.1		167772
26		167772		335544
27		335544		671089
28		671089		1.34218e+06
29		1.34218e+06		2.68435e+06
30		2.68435e+06		5.36871e+06
31		5.36871e+06		1.07374e+07
32		1.07374e+07		2.14748e+07
The total payment of your time working here will be $ 2.14748e+07


I entered 31 and the loop goes form 2 to 32, so there's is some problem with your counter

also last 5 values were in mantissa form so you need to work them too
Last edited on
For some reason, my professor makes me do them. He says it's good to do in programming. I think it's not necessary once you get out of the beginner range, but for now, we lose points if there are any unnamed numeric literals in our code.

My counter is 1 according to my code.

Isn't this correct?
1.
1
2
3
4
5
cout.precision(17);
while(counter <= days)
{
     // (. . . . .)
}


2.
1
2
3
4
5
6
7
8
9
10
11
while(counter <= days)
{
     // (. . . . .)

     counter = (counter + ONE_FOR_CALCULATIONS);

     cout << counter << "\t\t" << salary << "\t\t" << total << endl;
     totalPay = total;

     counter = (counter + ONE_FOR_CALCULATIONS);
} // end of while loop 

just change double counter = ONE_FOR_CALCULATIONS; to double counter =0

HOPE IT HELPS
Last edited on
Thanks!
welcome :D
closed account (E0p9LyTq)
@gentleguy,

FBHSIE is using symbolic constants instead of literals/"magic numbers." There are very valid reasons for using them, number #1 being readability.

Why are literals/magic number "bad?"
http://www.learncpp.com/cpp-tutorial/28-literals/

Symbolic constants:
http://www.learncpp.com/cpp-tutorial/2-9-symbolic-constants-and-the-const-keyword/

@FBHSIE, your instructor taught you some VERY GOOD advice, to use symbolic constants. :)

@p007,
why replace a symbolic constant with a literal/magic number? The OP declared a symbolic constant, just use that:
double counter = ZERO_FOR_CALCULATIONS;
+1 FurryGuy
If you're going to use "ZERO_FOR_CALCULATIONS", you may as well just use 0. Symbolic constants can be useful, but not when they're given bad names. A name that contains the value of the constant is useless. It's like comments such as
1
2
// increment i by 1
++i;
It's the difference between saying what is happening and saying why it's happening.
A name can also be bad if it's irrelevant.
1
2
const double superman = log(2.0);
const double batman = log(3.0);


Something better would be
1
2
double penny = VALUE_OF_A_PENNY;
double counter = INITIAL_COUNTER_VALUE;

For the specific case of a counter, I think this is perfectly acceptable:
1
2
3
double counter = 0;
//...
counter = counter + 1;
Last edited on
closed account (E0p9LyTq)
@helios,

The OP said his instructor told him to use all those symbolic constants:

For some reason, my professor makes me do them. He says it's good to do in programming. I think it's not necessary once you get out of the beginner range, but for now, we lose points if there are any unnamed numeric literals in our code
I think names such as ZERO_FOR_CALCULATIONS and ZERO_POINT_ZERO_ONE_FOR_CALCULATIONS are what is known as following the letter rather than the spirit of the rule.
Last edited on
Sorry, I'm not very good with names. We just can't have any unnamed literals, and sometimes the values used are merely values with no other meaning than to serve as a number which leaves me with less choices.
closed account (E0p9LyTq)
@FBHSIE,

You are following your instructor's rules, and that is what matters. :)

I might use different names, and not have everything be a symbolic constant, but that is me.

I would recommend you read the links I gave earlier to get a deeper understanding of the use of symbolic constants. And when NOT to use them in real C++ code.
Last edited on
Topic archived. No new replies allowed.