Money denomination code

Hi guys! I'm very new to coding (as in I can write the "Hello World!" program and a few other basic things.) For homework, we have to practice writing different programs. I've done all of them fairly easily but am stuck on the last one. Any hints you can give me? (Not looking for the full code to be done for me, just looking for an explanation or some hints!)


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main ()
{
   int cen, dol, qua, dim, nic, pen, left_over;
	
	cout << "Enter a number of change: ";
	cin >> cen;
}

Last edited on
closed account (E0p9LyTq)
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
#include <iostream>

int main()
{
   unsigned short dollars;
   unsigned short quarters;
   unsigned short dimes;
   unsigned short nickels;
   unsigned short cents;


   std::cout << "Enter a number of cents: ";
   std::cin >> cents;

   std::cout << "\nWith a total of " << cents << " cents there are:\n";

   dollars = cents / 100;
   cents %= 100; // get the remainder
   std::cout << dollars << " dollars.\n";

   quarters = cents / 25;
   cents %= 25;  // get the remainder
   std::cout << quarters << " quarters.\n";

   std::cout << cents << " cents.\n";
}


Enter a number of cents: 365

With a total of 365 cents there are:
3 dollars.
2 quarters.
15 cents.


You should be able to figure out the rest.

Another version that self-documents the number of cents in a dollar, quarter, etc.:

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

int main()
{
   unsigned short dollars;
   unsigned short quarters;
   unsigned short dimes;
   unsigned short nickels;
   unsigned short cents;

   const unsigned short CENTS_PER_DOLLAR  = 100;
   const unsigned short CENTS_PER_QUARTER = 25;
   const unsigned short CENTS_PER_DIME    = 10;
   const unsigned short CENTS_PER_NICKEL  = 5;

   std::cout << "Enter a number of cents: ";
   std::cin >> cents;

   std::cout << "\nWith a total of " << cents << " cents there are:\n";

   dollars = cents / CENTS_PER_DOLLAR;
   cents %= CENTS_PER_DOLLAR; // get the remainder
   std::cout << dollars << " dollars.\n";

   quarters = cents / CENTS_PER_QUARTER;
   cents %= CENTS_PER_QUARTER; // get the remainder
   std::cout << quarters << " quarters.\n";

   std::cout << cents << " cents.\n";
}
Last edited on
Generally start by mapping out on paper what you want to happen. In this case you ask for input in cents. So if someone were to type in for example 103 we know that the out put we want is 1 dollar and 3 pennies.

Your program should assign an int value to each type of money. A dollar is worth 100 for example. You can create a loop that checks first if the input value is greater than 100. If so subtract 100. A counter variable get increased by 1. We know we have at least 1 dollar. Run through the loop again. Is the value still greater than 100? No... Well then we only have one dollar. End loop.

Only 3 left.

Repeat this pattern down through the money values. Quarters, dimes, and nickels are all too big. Only three pennies left.

The output is 1 dollar and 3 pennies.

Last edited on
Topic archived. No new replies allowed.