Help with my code?

Hey guys, I have an assignment in which it requires me to add up how many quarters, dimes, nickels, and pennies I have. Now my problem is that how could I make it like if I put "3" when it asks me how many quarters, it would read it as 75 cents? same for the rest. In the prompt its asking me to find a way to add all these test data and the result would be $1.23 Sorry for my english

Test Data: 3 quarters, no dimes, 5 nickels, and 23 pennies

Can someone help me create a code which if I put 3 quarters, the system would translate it to 75. Same for nickels, if I put 5 nickels, it would translate it into 25. So in over all if I input 3 quarters, 5 nickels, and 23 pennies, it should add up to 1.23

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

int main()

{
 //Declare Variables
	int quarters;
	int dimes;
	int nickels;
	int pennies;
	float Z;

 //Input Variables
	cout << "How much quarters do you have?";
	cin >> quarters;
	cout << "How much dimes do you have?";
	cin >> dimes;
	cout << "How much nickels do you have?";
	cin >> nickels;
	cout << "How much pennies do you have?";
	cin >> pennies;

 //Compute
	Z = (quarters + dimes + nickels + pennies);

 //Output
	cout << Z;

	system("pause");
	return 0;


}
My suggestion would be to declare four constant variables and then do your calculations in main method. See. my code below. Hope this helps matey.

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
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

//constant 
//[Constants are expressions with a fixed value.]
const double qtrs = 0.25;
const double dm = 0.10;
const double nckl = 0.05;
const double pn = 0.01;

int main()
{
	//Declare Variables
	int quarters;
	int dimes;
	int nickels;
	int pennies;
	double z;
	
	//Input Variables
	cout << "How much quarters do you have?";
	cin >> quarters;
	cout << "How much dimes do you have?";
	cin >> dimes;
	cout << "How much nickels do you have?";
	cin >> nickels;
	cout << "How much pennies do you have?";
	cin >> pennies;
	
	//Compute
	z = (quarters * qtrs) + (dimes * dm) + (nickels * nckl) + (pennies * pn);

 //Output
	cout<<endl<<z;
	
	return 0;
}
Last edited on
@RonnieRahman's code is mostly fine. Don't use global variables though. Move all the code to inside of main.
@TarikNeaj
RonnieRahman does not use global variables. They are constants and using them global (even in an include file) is perfectly fine.
To expand on that - in C++, constant variables at file scope have internal linkage. So they're not "global" at all.
Cheers guys - hope OP (Mavs25) will understand this simple code.
Last edited on
Topic archived. No new replies allowed.