where am i going wrong?

I'm trying to assign values to 3 variables so that they represent coin vales, however when I try to enter to amount of coins of each type, quarters dimes and nickels, the program will not give the total value of the coins, instead just calculate the numbers I would enter in the program, for example, 1 quarter, 2 dimes and 3 nickels would value at 50 cents but instead output 6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{	
	int quarter = 25, dime = 10, nickel = 5, total= quarter + dime +nickel;
		cout << "please enter the number of quarters:\n";
		cin >> quarter;
		cout << "please enter the number of dimes:\n";
		cin >> dime;
		cout << "please enter the number of nickels:\n";
		cin >> nickel;

		total = quarter + dime + nickel;

		cout << "you have ";
		cout << total;
		cout << " cents in total \n";
	return 0;
}
Last edited on
closed account (LN7oGNh0)
On line 14, you say what 'total' is again. Why not taking away the first one and then putting int in front of the one in line 14.
int quarter = 25, dime = 10, nickel = 5, total= quarter + dime +nickel;

total doesn't get assigned here. Consider just setting total to zero.

You'll want to multiply the value of each coin by the amount that the person has:

quarter *= 25;
dime *= 10;
nickel *= 5;

total = quarter + dime + nickel;
wouldn't that just change the name of the variables?
what your cin>>quarter is doing is reassigning the value of quarter to whatever the user puts in as how many quarters they own.
what you should do is make three new variables like NumQuarter, numDime, numNickel for user input and then multiply those by quarter, dime, and nickel to get the right values.
Or, since the value of a coin is something that's not likely to ever change, you can do it the way pogrady does.

...EDIT...

I just took another look at your code and your question. I think the problem you're really having is that you need to understand that a code has a predictable flow to it, it starts from the top line and works it's way down. If you give a variable a new value then the previous value that you assigned above that is now lost, just gone. The variable quarter does not remember that it once equaled 25 cents, it just knows that you've now set it equal to whatever number the user inputs.
Last edited on
pogrady is just simply adding a "*" to the variable's name is it not? I removed the equation on line 6 and kept line 14 but still no joy
Last edited on
*= is an operator, it is called "times equal", it works the same as += except with multiplication instead of addition.
1
2
3
4
5
var += 5; // var plus equal 5 is the same as writing out
var = var +5; //var equals itself plus 5.

var *= 5; //is the same as
var = var * 5;
here's a miniature version of what your code might look like;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#inlcude <iostream>
using namespace std;
int main()
{
char answer;
double quarter = .25, numQ=0, total = 0;
cout<<"enter the number or quarters you have"<<endl;
cin>>numQ; //how many quarters they have
total = quarter*numQ; //now we have all the right information we can use the equation
cout<<endl<<"You have $"<<total<<endl;

cout<<"Hit Enter to end";
cin>>answer; //windows visual C++ doesn't pause at the end of a program, so we do it manually.
return 0;
}


I used double instead of int so that you can express the value in dollars.
Last edited on
The book I'm trying to use is jumping way ahead with the project in comparison to the notes, need to read up on a couple of items you have used but thank you for your help
Topic archived. No new replies allowed.