Converting Dollars to Coins

I have been working on a program that Converts Dollars to coins. In my code it has to have multiple outputs to the screen, at least one input, the use of integers and strings, repetition with Do..While, If..Else, must have some output text to show correct value of coins that would be converted from the dollars, code must include comments explaining your reason for the code section or what the code is doing, code must compile whole dollars only. If value is less than 1 or 0, the program should break or exit. So far I have come up with this but get warning C4700: uninitialized local variable 'm' used and the same for warning C4700: uninitialized local variable 'c' used. Here is what I have:


#include <iostream>
using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl


#define pennies 1 // tells the code what pennies value is
#define dime 10 // tells the code what dime value is
#define quarter 25 // tells the code what dime value is
#define nickel 5 // tells the code what nickel value is


int main() // function main begins program execution
{
int m; // creates integer for dollar input
int p = pennies; // creates integer for pennies
int d = dime; // creates integer for dime
int q = quarter; // creates integer for quarter
int n = nickel; // creates integer for nickel
int c; // creates integer for coin input
int amount = ( m * 100 / c ); //answer for amount is m * 100 / c

cout << "Please enter in a dollar amount to convert it to change"; //prompts user
cin >> m; // user enters dollar amount
system("pause"); // keep window open to enter in c
cout << "To determine how many penny's, nickels, dimes or quarters, enter\n"; //display message
cout << "pennies or nickel or dime or quarter\n"; // choice of coins
cin >> c; //user inputs name of coin
{
if ( m <= 1 ) // if dollar amount if 1 or less end loop
return 0;
}
{
if ( c == pennies )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
else if ( c == nickel )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
else if ( c == dime )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
else if ( c == quarter )
return ( amount = m * 100 / c ); // calculate dollar and coin amount
}
cout << "The result is:" << amount <<"'s in" << c << endl; // display message with amount from m * 100 / c

system("pause"); // keep window open


return 0; // indicates successful termination
}

I am sure I am screwing this up big time, and I am lost. I am new to this 4 weeks into learning C++. Please tell how I am screwing this up, any help or ideas would be appreciated.
Last edited on
Im working on it, I cant quite figure out whats wrong, as I am semi-new myself. But I saw that noone else replied so dedicated myself to help you figure it out. right now its saying The variable 'm' is being used without being initialized. Same with C.. But youve clearly placed them as "int m" "int c" So im not for sure.. =/
If you could find some way to #define them, I think thatd solve the problem.
Last edited on
closed account (3hM2Nwbp)
Posts that don't use code tags go to the back of the line in my book. It may be harsh, but I feel it's justified. Some comments:

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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

//#include <string> <-- Not Needed

/// Use the preprocessor sparingly.
/*#define pennies 1
#define dime 10
#define quarter 25
#define nickel 5*/

int main()
{
  // Changed macros to constants
  const int PENNY = 1;
  const int NICKEL = 5;
  const int DIME = 10;
  const int QUARTER = 25;
  int money; // <-- Gave it a more descriptive name
/*int p = pennies;
int d = dime;
int q = quarter;
int n = nickel;*/ // <-- Not Needed
//int c;
//int amount; /* = ( m * 100 / c );*/ /// <-- m and c contain garbage, don't assign

  cout << "Please enter in a dollar amount to convert it to change";
  cin >> money; // <-- Reminder: Input is not validated.
//system("pause"); <-- Not needed
//cout << "To determine how many penny's, nickels, dimes or quarters, enter\n";
//cout << "pennies or nickel or dime or quarter\n";
//cin >> c;
//cout << ( m * 100 / c ) << endl;
  if ( money < 1 ) // Changed this line ( if m is less than 1)
    return -1; // Return a non-zero to indicate failure
/*if ( c == pennies )
return ( amount = m * 100 / c );
else if ( c == nickel )
return ( amount = m * 100 / c );
else if ( c == dime )
return ( amount = m * 100 / c );
else if ( c == quarter ) return ( amount = m * 100 / c );
}*/
  cout << "Number of pennies: " << ((money * 100) / PENNY);
  cout << "Number of nickels: " << ((money * 100) / NICKEL);
  cout << "Number of dimes: " << ((money * 100) / DIME);
  cout << "Number of quarters: " << ((money * 100) / QUARTER);
//  cout << "The result is:" << amount <<"'s in" << c << endl;

//  system("pause");
  return 0; // Return zero, indicating a successful run.
}


Cleaned Up:

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

int main()
{
  const int PENNY = 1;
  const int NICKEL = 5;
  const int DIME = 10;
  const int QUARTER = 25;
  int money;
  std::cout << "Please enter in a dollar amount to convert it to change ";
  std::cin >> money;
  if ( money < 1 )
  {
     return -1;
  }
  std::cout << "\nNumber of pennies in " << money << " dollars: " << ((money * 100) / PENNY) << std::endl;
  std::cout << "Number of nickels in " << money << " dollars: " << ((money * 100) / NICKEL) << std::endl;
  std::cout << "Number of dimes in " << money << " dollars: " << ((money * 100) / DIME) << std::endl;
  std::cout << "Number of quarters in " << money << " dollars: " << ((money * 100) / QUARTER) << std::endl;
  return 0;
}
Last edited on
Thanks Luc for the response and thank you for the example. If I wanted to have the user input a dollar amount and then also input what coin to divide by how would I do that? I made some changes per your recommendations and example to mine, now I only get one warning, : warning C4700: uninitialized local variable 'total' used.

#include <iostream>
using std::cout; // program uses cout
using std::cin; // proram uses cin
using std::endl; // program uses endl


int main() // function main begins program execution
{
int money; // creates integer for money input
const int penny = 1; // creates constant integer for penny
const int dime = 10; // creates constant integer for dime
const int quarter = 25; // creates constant integer for quarter
const int nickel = 5; // creates constant integer for nickel
int coin; // creates integer for coin input
int total; //answer for amount is m * 100 / c

cout << "Please enter in a dollar amount to convert it to change"; //prompts user
cin >> money; // user enters dollar amount

cout << "To determine how many penny's, nickels, dimes or quarters, enter\n"; //display message
cout << "pennies or nickel or dime or quarter\n"; // choice of coins
cin >> coin; //user inputs name of coin
{
if ( money <= 1 ) // if dollar amount if 1 or less end loop
return -1;
else if ( coin == penny )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
else if ( coin == nickel )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
else if ( coin == dime )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
else if ( coin == quarter )
return ( total = ( money * 100 ) / coin ); // calculate dollar and coin amount
}
cout << "The result is:" << total <<"'s in" << coin << endl; // display message with amount from m * 100 / c

system("pause"); // keep window open


return 0; // indicates successful termination
}
WHY DOES NO ONE USE CODE TAGS?? He just said that posts that don't use code tags go to the back of the line in his book. Well it's like that for 90% of this community and seeing as how he JUST said that I would think your next post would have code tags.
I apologize about that, I am still learning about using this forum. I need to read about using code tags.
I hope this is correct, I read the example from" Put your code in code tags" forum. I also need to keep my if...else in the code example.

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
#include <iostream>
using std::cout; // program uses cout
using std::cin; // proram uses cin
using std::endl; // program uses endl


int main() // function main begins program execution
{
	int money; // creates integer for money input
	const int penny = 1; // creates constant integer for penny
	const int dime = 10; // creates constant integer for dime
	const int quarter = 25; // creates constant integer for quarter
	const int nickel = 5; // creates constant integer for nickel
	int coin; // creates integer for coin input
	int total; //answer for amount is m * 100 / c
	
	cout << "Please enter in a dollar amount to convert it to change"; //prompts user 
		cin >> money; // user enters dollar amount
	
	cout << "To determine how many penny's, nickels, dimes or quarters, enter\n"; //display message
	cout << "pennies or nickel or dime or quarter\n"; // choice of coins
		cin >> coin; //user inputs name of coin then coin total is displayed next to "The result is:"
		{
	if ( money <= 1 ) // if dollar amount if 1 or less end loop
	 return -1;
		else if ( coin == penny ) // if user types penny then
		   return ( total = ( money * 100 ) / coin ); // divide money by coin
              else if ( coin == nickel ) // if user types nickel then
                 return ( total = ( money * 100 ) / coin ); // divide money by coin 
                    else if ( coin == dime ) // if user types dime then
	                   return ( total = ( money * 100 ) / coin ); // divide money by coin
	                      else if ( coin == quarter ) //if user types quarter
	                         return ( total = ( money * 100 ) / coin ); // divide money by coin
		}
cout << "The result is:" << total <<"'s in" << coin << endl; // display message with amount from m * 100 / c

system("pause"); // keep window open
	

return 0; // indicates successful termination
}
closed account (3hM2Nwbp)
What do you want the user to enter at the menu? As it's written, to select pennies, the user types 1, for nickels they enter 5, dimes enter 10, quarters enter 25.

Most menus give a print-out of choices like so:


MENU
0) Cancel
1) Penny
2) Nickel
3) Dime
4) Quarter
Please Enter Selection: 
either a word or number works for me, just as long as the user has to type the money in, then type the coin. I had put the pause earlier in the code at the beginning in a hopes of some how pausing the display window so the user could type in the money and coin, but could not get it to build. I would say a number corresponding to the coin amount would work best.
closed account (3hM2Nwbp)
Some more food for thought.

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

int main()
{
  // Common convention is that constants are all capital
  const int PENNY = 1;
  const int NICKEL = 5;
  const int DIME = 10;
  const int QUARTER = 25;
  
  int money;
  int coin;
	
  cout << "Please enter in a dollar amount to convert it to change ";
  cin >> money;
	
  cout << "MENU\n"
       << "1) Pennies\n"
       << "2) Nickels\n"
       << "3) Dimes\n"
       << "4) Quarters" << endl;
       
  char choice;
  cin >> choice;
  
  switch(choice)
  {
    case '1':
      // Do Pennies Here
      coin = PENNY;
      break;
    case '2':
      // Do Nickels Here
      coin = NICKEL;
      break;
    case '3':
      // Do Dimes Here
      coin = DIME;
      break;
    case '4':
      // Do Quarters Here
      coin = QUARTER;
      break;
    default:
      // If the user entered an invalid choice handle it here.
      return -1;
  }
  cout << "The result is: " << ((money * 100) / coin) << " in " << money << " dollars." << endl;
  return 0;
}
OK I so I received no errors or warnings after I figured out how to initialize "total" is set total = 0. I can get all the outputs to show up, but everytime I enter in a dollar value and hit return it go right to the result is 0 instead of next prompting me to enter in a choice. Do I have to write an if statement to go between where I enter money and enter a choice for coin? Thanks for the tip on convention, I have to remember to follow suit.

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
#include <iostream>
using std::cout; // program uses cout
using std::cin; // proram uses cin
using std::endl; // program uses endl


int main() // function main begins program execution
{
	int money; // creates integer for money input
	const int PENNY = 1; // creates constant integer for penny
	const int DIME = 10; // creates constant integer for dime
	const int QUARTER = 25; // creates constant integer for quarter
	const int NICKEL = 5; // creates constant integer for nickel
	int total = 0; //answer for amount is money * 100 / choice
	
	cout << "Please enter in a dollar amount to convert it to change\n"; //prompts user 
		cin >> money; // user enters dollar amount
	
	cout << "To determine how many pennies, dimes, quarters or nickels, enter\n"; //display message
	cout << "MENU\n"
       << "1) Pennies\n"
       << "2) Dimes\n"
       << "3) Quarters\n"
       << "4) Nickels" << endl;

	char choice;
	cin >> choice;
		{
	if ( money <= 1 ) // if dollar amount if 1 or less end loop
	 return -1;
		else if ( choice == 1 ) // if user types penny then
		   return ( total = ( money * 100 ) / PENNY ); // divide money by coin
              else if ( choice == 2 ) // if user types nickel then
                 return ( total = ( money * 100 ) / DIME ); // divide money by coin 
                    else if ( choice == 3 ) // if user types dime then
	                   return ( total = ( money * 100 ) / QUARTER ); // divide money by coin
	                      else if ( choice == 4 ) //if user types quarter
	                         return ( total = ( money * 100 ) / NICKEL ); // divide money by coin
		}
cout << "The result is:" << total <<"'s in" << choice << endl; // display message with amount from m * 100 / c

system("pause"); // keep window open
return 0; // indicates successful termination
}
Last edited on
Topic archived. No new replies allowed.