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.