Help on price calculation

I can't seem to get this code to work out correctly. No matter what value the user enters, it always totals out to $1.41. I understand why its doing that now (I believe), but I can't figure out a way around it. Here is the code.

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
                double dollars = 1.00;
		double quarters = 0.25;
		double dimes = 0.10;
		double nickels = 0.05;
		double pennies = 0.01;

		double total;
		total = dollars + quarters + dimes + nickels + pennies;

		Sleep(500);
		cout << "Please enter the amount you will pay in this order:\n";
		Sleep(500);
		cout << "Dollars, Quarters, Dimes, Nickles, Pennies.\n";
		Sleep(500);
		cout << "Here is an example:\n";
		Sleep(500);
		cout << "10 1 1 1 10" << endl;
		Sleep(500);
		cout << "This reads: 10 Dollars, 1 Quarter, 1 Dime, 1 Nickel, and 10 Pennies. Which equals, $10.50" << endl;
		Sleep(500);
		cout << "Please enter the amount:" << endl;
		
		// THIS BLOCK OF CODE IS NOT WORKING CORRECTLY
		// NO MATTER WHAT NUMBERS THE USER ENTERS, THE TOTAL VALUE           //ALWAYS EQUALS $1.41. 
		cin >> dollars;
		cin.ignore();
		
		cin >> quarters;
		cin.ignore();
		
		cin >> dimes;
		cin.ignore();
		
		cin >> nickels;
		cin.ignore();
		
		cin >> pennies;
		cin.ignore();


		cout << "You have paid $" << total << endl;



		return 0;
	
}
Last edited on
1
2
3
4
5
6
7
8
double dollars = 1.00;
double quarters = 0.25;
double dimes = 0.10;
double nickels = 0.05;
double pennies = 0.01;

double total;
total = dollars + quarters + dimes + nickels + pennies;
Let me unroll this for you:
total = 1.00 + 0.25 + 0.10 + 0.05 + 0.01 = 1.41

After that total never changes and so it remains the same.

Example of what happens:
1
2
3
4
int x = 1; //x == 1
int y = x; //x == 1, y == 1 
    x = 2; //x == 2, y == 1
    y = 5; //x == 2, y == 5; 
Last edited on
Two main problems with your program.

First, you need to actually have two types of variables. One is a const float (or const double, if you want), which is the actual value of a quarter, dollar, dime, etc. The other variable you need is the number of currency pieces that the user has. What you're basically doing right now is asking what the VALUE of a dollar/quarter/etc. is, not how many they have. So do something like this:

1
2
3
4
5

const double dollars = 1.00; //const not really necessary, but can put in
int dollarsCount;                   //number of dollars the user inputs
...
cin >> dollarsCount;


Do you see the issue? You've declared what the value of a currency type is, and then you completely rewrite that value with the cin >> statements.

The other issue is this:

1
2
3
4
5
6
7
8
double dollars = 1.00;
		double quarters = 0.25;
		double dimes = 0.10;
		double nickels = 0.05;
		double pennies = 0.01;

		double total;
		total = dollars + quarters + dimes + nickels + pennies;


What this does is it sets the value of a quarter, the value of a dime, ..., etc., and then says the total is the sum of all those numbers. What the total is, AS YOU"VE CALCULATED IT, is just 1 quarter + 1 dime + ... This is obviously not what you want. What you want to do is create a more accurate formula given the new variables I've laid out for you above:

This is my suggestion for a complete code. Please study it and try to make sure you understand it all, as there are some fundamental mistakes you are making:

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
//include statements, main, etc.

const double dollars = 1.00;
const double quarters = 0.25;
const double dimes = 0.10;
const double nickels = 0.05;
const double pennies = 0.01;
double total;
int dollarsCount;
int quartersCount;
int dimesCount;
int nickelsCount;
int penniesCount;

//cout statements that you have above are fine

cin >> dollarsCount;
cin.ignore();

cin >> quartersCount;
cin.ignore();

cin >> dimesCount;
cin.ignore();

cin >> nickelsCount;
cin.ignore();

cin >> penniesCount;
cin.ignore();

total = dollarsCount*dollars + quartersCount*quarters + dimesCount*dimes + nickelsCount*nickels + penniesCount*pennies;

cout << "You have paid $" << total << endl;

return 0;

//end main stuff


Notice that line total = dollarsCount*dollars + ... ... This is an important mistake you are making. The value of a variable does not constantly update unless you tell it do. For example, if I say

[code]
int a = 1;
int b = 2;

int c = a + b;
cout << c << "\n";
int a = 2;
int a = 3;

cout << c << "\n";
[code]

The output will be 3, 3 both times. This is because when I set c = a + b it only reflects the values of a and b at the time of the assignment, it doesn't follow them around. It would be nice if there was a variable that did this. In fact, there is; it's called a pointer and you'll learn about it later on :)

Let me know if you have any other questions or if you don't understand something.
Thank you all for responding. After overlooking all your solutions I was able to work this out.

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

		double dollars = 1.00;
		double quarters = 0.25;
		double dimes = 0.10;
		double nickels = 0.05;
		double pennies = 0.01;

		double dolAmount;
		double qrtAmount;
		double dimAmount;
		double nikAmount;
		double penAmount;

		double total;

		Sleep(500);
		cout << "Please enter the amount you will pay in this order:\n";
		Sleep(500);
		cout << "Dollars, Quarters, Dimes, Nickles, Pennies.\n";
		Sleep(500);
		cout << "Here is an example:\n";
		Sleep(500);
		cout << "10 1 1 1 10" << endl;
		Sleep(500);
		cout << "This reads: 10 Dollars, 1 Quarter, 1 Dime, 1 Nickel, and 10 Pennies. Which equals, $10.50" << endl;
		Sleep(500);
		cout << "Please enter the amount:" << endl;
		
		cin >> dolAmount;
		cin.ignore();
		
		cin >> qrtAmount;
		cin.ignore();
		
		cin >> dimAmount;
		cin.ignore();
		
		cin >> nikAmount;
		cin.ignore();
		
		cin >> penAmount;
		cin.ignore();

		//cout.precision()

		total = (dollars * dolAmount) + (quarters * qrtAmount) + (dimes * dimAmount) + (nickels * nikAmount) + (pennies * penAmount);

		cout << "You have paid $" << total << endl;
You can drop .ignore() calls. >> is an formatted input.
Will do. Thank you MiiNiPaa.
Topic archived. No new replies allowed.