Adding float and int

I am writing this program and trying to get the values of dollar, quarter, dime, nickel, and penny to add for the total money value. When the program runs, and adds them together, it adds up to an int with no decimal. Any idea what I am doing wrong?

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

int main()
{
	string name;
	int dollar;
	float quarter, dime, nickel, penny;
	double total;

	


	dollar = 1;
	quarter = .25;
	dime = .1;
	nickel = .05;
	penny = .01;
	


	// input servers name
	cout << "What is your name?";
		getline(cin, name);
	

	//input dollars made
		cout << "how many dollars did you receive";
		cin >> dollar; 
	

	// input quarters made
	cout << "Quarters received?";
	cin >> quarter;

	// input dimes
	cout << "dimes";
	cin >> dime;
	
	// input nickel 
	cout << "nickels?";
		cin >> nickel;

	// input penny
	cout << "pennies";
	cin >> penny;

	// input total
	total = dollar + quarter + dime + nickel + penny;
	cout <<total;


	system("PAUSE");
	return 0;

}
Last edited on
@TheArk

You are not getting the monetary values of the coins. For that, you would need to multply the coins received by the coin value. ie: quarter = .25. Quarters received = 3. 3x.25 = .75.
All you're doing is taking the integer values of coin amounts, and adding them together, so, yes, you will get an integer answer. Use a different variable for coins received, then do the math on their values times the coins in hand.
right on, thanks!
Topic archived. No new replies allowed.