Holding Variable

OK I have tried this 100 different ways but I keep getting same result it is holding variable instead of clearing and let user re-enter new numbers for a new variable to be checked.

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
  #include <iostream>
using namespace std;

int main()
{
	double  PENNY = .01,
			NICKELS = .05,
			DIMES = .10,
			QUARTERS = .25,
			uPennies=0,uNickels = 0,uDimes = 0, uQuarters = 0;
	
	cout << "Enter number of pennies>> " << flush,cin >>uPennies ;
	cout << "Enter number of nickels>> " << flush, cin >> uNickels;
	cout << "Enter number of dimes>> " << flush, cin >> uDimes;
	cout << "Enter number of quarters>> " << flush, cin >> uQuarters;

	uPennies *= PENNY;
	uNickels *= NICKELS;
	uDimes *= DIMES;
	uQuarters *= QUARTERS;

	double total;
	total = uPennies+uNickels+uDimes+uQuarters;

	if (total == 1.00)
	{
		cout << "Hooray your amazing you did it....Way to go! " << endl;
	}

	else if (total > 1.00)
	{
		cout << "Your guess was too high try again...." << endl;
	}
	else
		cout << "Your guess was too low try again...." << endl;
	
	while (total != 1.00)

	{
		cout << total<<endl;
		cout << "Enter number of pennies>> "<<flush, cin >> uPennies;
		cout << "Enter number of nickels>> " << flush, cin >> uNickels;
		cout << "Enter number of dimes>> " << flush, cin >> uDimes;
		cout << "Enter number of quarters>> " << flush, cin >> uQuarters;
	
	}
	return 0;
}


Enter number of pennies>> 1
Enter number of nickels>> 1
Enter number of dimes>>
1
Enter number of quarters>> 1
Your guess was too low try again....
0.41
Enter number of pennies>> 5
Enter number of nickels>> 5
Enter number of dimes>> 5
Enter number of quarters>> 5
0.41
Enter number of pennies>> 0
Enter number of nickels>> 0
Enter number of dimes>> 0
Enter number of quarters>> 4
0.41
Enter number of pennies>>


I know I am missing something easy just not sure what it is?
Hello briancb2004,

You might want to consider this for the beginning of your program:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    constexpr double 
        PENNY = .01,
        NICKELS = .05,
        DIMES = .10,
        QUARTERS = .25;

    double uPennies{}, uNickels{}, uDimes{}, uQuarters = 0;  // <--- If you use this last 1 it should 0.0. 


Do not try to be fancy until you understand the language better. This:
cout << "Enter number of pennies>> " << flush,cin >>uPennies ; would be better written as:
1
2
cout << "Enter number of pennies>> ";
cin >>uPennies ;

The use of the comma is not working the way that you think it is. The "flush" is not needed because the "cin" will flush the output buffer before t takes in any input.

Putting the "cout" and "cin" on 1 line is not helping you. First understand how it is done then learn how to shorten it. You are not writing the code for the compiler, but for your-self and others so it can be easily read and understood.

Once I get this loaded up in my IDE I see what else I can find.

Andy
You shouldn't compare floating-point numbers for exact equality (especially not money). They are inexact by their nature, just like real-life measurements.

I suggest doing all your math using integers (nickel = 5, dime = 10, etc.) and make sure the quantities add up to 100.

Second, you never re-calculate total within your loop, so your while loop starting on line 37 is potentially an infinite loop.
Hello briancb2004,

Ganado wrote:

so your while loop starting on line 37 is potentially an infinite loop.


I do not see this as being potentially it is an infinite loop because total never changes, so you are always comparing the same value for "total".

A while loop can work, but I used a do/while loop:
1
2
3
4
5
6
7
8
9
10
11
double uPennies{}, uNickels{}, uDimes{}, uQuarters{};
double total{};  // <--- Needs to be here to work properly.
    
do
{
    cout << "Enter number of pennies>> ";
    cin >> uPennies;

// ...

 } while (total != 1.00);


This produces the output of:

Enter number of pennies>> 5
Enter number of nickels>> 3
Enter number of dimes>> 2
Enter number of quarters>> 2

    Your guess was too low try again....

Enter number of pennies>> 5
Enter number of nickels>> 3
Enter number of dimes>> 3
Enter number of quarters>> 2


    Hooray your amazing you did it....Way to go!


And if you are high or low it would keep repeating until you get it right.

Using the do/while loop you can eliminate the whole while loop.

Andy
Do While worked. Truthfully it was one of the 100 things I tried and was getting same result. But this time I must have done something right. Thanks I usually write it that way because it's easier on me to read. Didn't realize the comma could be messing things up. Thanks for the tip. I tried it without and it worked. I just tried inserting this code and it worked too. Not sure what it's doing but I will do some research thanks for the help.

1
2
3
4
5
6
7
 constexpr double 
        PENNY = .01,
        NICKELS = .05,
        DIMES = .10,
        QUARTERS = .25;

    double uPennies{}, uNickels{}, uDimes{}, uQuarters = 0;  // <--- If you use this last 1 it should 0.0. 


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


#include <iostream>
using namespace std;

int main()
{
	double  PENNY = .01,
			NICKELS = .05,
			DIMES = .10,
			QUARTERS = .25,
			uPennies,uNickels,uDimes, uQuarters;
		double total;
		
	do
	{
		cout << "Enter number of pennies>> ";
		cin >> uPennies;
		cout << "Enter number of nickels>> ";
		cin >> uNickels;
		cout << "Enter number of dimes>> ";
		cin >> uDimes;
		cout << "Enter number of quarters>> ";
		cin >> uQuarters;
		
		uPennies *= PENNY;
		uNickels *= NICKELS;
		uDimes *= DIMES;
		uQuarters *= QUARTERS;

		total = uPennies + uNickels + uDimes + uQuarters;

		
		if (total == 1.00)
		{
			cout << "Hooray your amazing you did it....Way to go! " << endl;
		}

		else if (total > 1.00)
		{
			cout << "Your guess was too high try again...." << endl;
		}
		else
		{
			cout << "Your guess was too low try again...." << endl;
		}
		cout << total << endl;
	} while (total != 1.00);

		cout << "Enter number of pennies>> "<<flush, cin >> uPennies;
		cout << "Enter number of nickels>> " << flush, cin >> uNickels;
		cout << "Enter number of dimes>> " << flush, cin >> uDimes;
		cout << "Enter number of quarters>> " << flush, cin >> uQuarters;
	
	
	return 0;
}



Enter number of pennies>> 5
Enter number of nickels>> 5
Enter number of dimes>> 5
Enter number of quarters>> 5
Your guess was too high try again....
2.05
Enter number of pennies>> 1
Enter number of nickels>> 1
Enter number of dimes>> 1
Enter number of quarters>> 1
Your guess was too low try again....
0.41
Enter number of pennies>> 0
Enter number of nickels>> 0
Enter number of dimes>> 0
Enter number of quarters>> 4
Hooray your amazing you did it....Way to go!
1
Enter number of pennies>>
Thanks Ganado will keep that in mind
Too pedantic. I said "potentially" because it's possible the loop wouldn't be triggered at all, that's it.

@brian np. Floating-point equality can work in some cases, but it's best not to rely on it.
Last edited on
@Ganado,

Thank you. I see your point now and realize I was looking at it in a different way. In the future I will work on considering more possibilities.

Hello briancb2004,

Sorry for the delay.

For the code:
1
2
3
4
5
6
7
8
constexpr double
    PENNY{ 0.01 },
    NICKELS{ 0.05 },
    DIMES{ 0.10 },
    QUARTERS{ 0.25 };

double uPennies{}, uNickels{}, uDimes{}, uQuarters{};
double total{};

"constexpr" is available from C++11 on and is the preferred way to make a numeric variable a constant. The older form is just "const" and either will work.

The fact that the variable names are in capital letters tends to imply that they are constant variables whose value can not be changed. This is helpful when the compiler finds you are trying to change the value of a constant variable and stops with an error.

Line 7 defines the non-constant variables whose values do change in the program.

The {}s, available from C++11 on, known as the uniform initializer make it very easy to initialize your variables. The above code demonstrates 2 ways to use the uniform initializer. The empty {}s will set the variable to the type of zero needed based on the variables type.

Lastly when giving a "double" variable a value it is best to use "0.0" or "0.25" although "0" or ".02" will work it is more proper to use the former.

Using the last code you posted I have made a few adjustments for you to consider:
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    constexpr double
        PENNY{ 0.01 },
        NICKELS{ 0.05 },
        DIMES{ 0.10 },
        QUARTERS{ 0.25 };

    double uPennies{}, uNickels{}, uDimes{}, uQuarters{};
    double total{};

    std::cout << std::fixed << std::setprecision(2);

    do
    {
        cout << "Enter number of pennies>> ";
        cin >> uPennies;

        cout << "Enter number of nickels>> ";
        cin >> uNickels;

        cout << "Enter number of dimes>> ";
        cin >> uDimes;

        cout << "Enter number of quarters>> ";
        cin >> uQuarters;

        uPennies *= PENNY;
        uNickels *= NICKELS;
        uDimes *= DIMES;
        uQuarters *= QUARTERS;

        total = uPennies + uNickels + uDimes + uQuarters;


        if (total == 1.00)
        {
            cout << "\n\n    Hooray your amazing you did it....Way to go!\n\n";
        }

        else if (total > 1.00)
        {
            cout << "\n    Your guess was too high try again....\n\n";
        }
        else
        {
            cout << "\n    Your guess of " << total << " was too low try again....\n\n";
        }

        //cout << total << endl;  // <--- OK for testing, but no longer needed.
    } while (total != 1.00);

    return 0;
}

Line 2 I added for the use with the output. You do not have to include this header for the program to work, but you would have to comment line 17 or remove it.

Starting at line 21 and using the blank lines makes the code easier to follow and read.

Line 52 I changed. See what you think. Line 48 would work better if it matches the format of line 52. Line 43 is up to you if you want to do something different. I like to put some leading spaces at the beginning of some of the output just to make it look different. This part is up to you how you do it.

This is another version that would be more acurate than using "double"s:
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
#include <iostream>

using namespace std;

int main()
{
    constexpr int
        PENNY{ 1 },
        NICKELS{ 5 },
        DIMES{ 10 },
        QUARTERS{ 25 };

    int uPennies{}, uNickels{}, uDimes{}, uQuarters{};
    int total{};

    do
    {
        cout << "Enter number of pennies>> ";
        cin >> uPennies;

        cout << "Enter number of nickels>> ";
        cin >> uNickels;

        cout << "Enter number of dimes>> ";
        cin >> uDimes;

        cout << "Enter number of quarters>> ";
        cin >> uQuarters;

        uPennies *= PENNY;
        uNickels *= NICKELS;
        uDimes *= DIMES;
        uQuarters *= QUARTERS;

        total = uPennies + uNickels + uDimes + uQuarters;


        if (total == 100)
        {
            cout << "\n\n    Hooray your amazing you did it....Way to go!\n\n";
        }

        else if (total > 100)
        {
            cout << "\n    Your guess of " << total / 100 << '.'
                 << (total % 100 < 10 ? "0" : "") << total % 100 << " was too high try again....\n\n";
        }
        else
        {
            cout << "\n    Your guess of " << total / 100 << '.'
                 << (total % 100 < 10 ? "0" : "") << total % 100 << " was too low try again....\n\n";
        }

        //cout << total << endl;  // <--- OK for testing, but no longer needed.
    } while (total != 100);

    return 0;
}


Compare and see what you think.

Andy
Topic archived. No new replies allowed.