ONE INPUT TO THE NEXT REQUIRED FOR ANSWER

I am trying to figure out how to write a code that requires the user to enter in one input and then the code requires the user to input one additional choice. The rest of my code is set up to answer based on what the user inputs. This is where I need help:

1
2
3
4
5
6
7
8
9
10
11
cout << "Please enter in a dollar amount to convert it to change\n"; //prompts user 
		cin >> money; // user enters dollar amount

/**********after user inputs money, code should then ask what type of coin*****/

	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;



Here is the entire code I have written:

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 chooses 1 for penny then
		   return ( total = ( money * 100 ) / PENNY ); // divide money by coin
              else if ( choice == 2 ) //if user chooses 2 for dime then
                 return ( total = ( money * 100 ) / DIME ); // divide money by coin 
                    else if ( choice == 3 ) //if user chooses 3 for quarter then
	                   return ( total = ( money * 100 ) / QUARTER ); //divide money by coin
	                      else if ( choice == 4 ) //if user chooses 4 for nickel
	                         return ( total = ( money * 100 ) / NICKEL ); //divide money by coin
		}
cout << "The result is:" << total <<"'s in" << choice << endl; // display message with amount from money * 100 / choice

system("pause"); // keep window open
return 0; // indicates successful termination
}
"money" and all of the coins should be floats, I'd think that would be a given when dealing with change. To answer you question a function would be the best way to deal with this.
1
2
3
4
5
6
7
8
float total(float value)
{
      float number;
      cout << "How many?";
      cin >> number;
      
      return (number * value);
}
I am assuming that I have to write a do...while statement to go to the next input. Something like this, but this still won't give me the option to do one input then the next input.

1
2
3
4
5
6
7
8
9
10
11
do
	{
	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;
	cin >> choice;

	}while (money <= 1 );
"while money is less then or equal to one"? Don't worry I make that mistake all the time to.

You would put that function call inside that loop and keep a running total in another variable.
@Computergeek01: Don't use floating point arithmetic when dealing with money, (round off error)

30
31
32
//inside main
		else if ( choice == 1 )
		   return ( total = ( money * 100 ) / PENNY ); // you are terminating the program here 

Also, those ifs are not anidated, ¿why are you indenting them in that way?
Use double's then, just don't use an integer. I had actually forgotten about the floating point round off issue... My bad.
I second using integers. Doubles are still floating points.
Is that specific to this instance because we're not dividing? I can easily see rounding errors by using straight ints as well. The only thing that would yeild an accurate result by those standards is a custom data type.
Why not use an int to represent to number of pennies total and just do math around that??
Floating point arithmetic poses a bigger problem by having no way to represent certain exact values, such as 0.1. You can use the integer unit to mean 10^-5 of the monetary unit with 64-bit integers and still have enough space to represent all the money in the world a couple hundred times.
@Computergeek01 - I made the change on the money <= 1 to money >= 1 and now when I press enter I can go to the next input line, but it repeats 4 times and everytime I choose a number it goes back to the menu options. So it looks like this now, but I cant get past the menu. I want to take the money and choice and then have the if...else statements handle rest.

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
#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
	char choice;

	cout << "Please enter in a dollar amount to convert it to change\n"; //prompts user 
		cin >> money; // user enters dollar amount
		
	do
	{
	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;
	cin >> choice;

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

system("pause"); // keep window open
return 0; // indicates successful termination
}


Last edited on
I apologize if I am making bad mistakes, but I really want to learn how to properly make this code. I am using Deitel & Deitel How to Program for examples and definitions.
@ne555 - I made changes on the if...else. I made it read this way now, but what do I put in place of return so my if...else returns those values if the statement is true? I also can't test this since I cant get past the menu portion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if ( money <= 1 ) // if dollar amount if 1 or less end loop
	 return -1;
		else; 
			if ( choice == 1 ) //if user chooses 1 for penny then
		       ( total = ( money * 100 ) / PENNY ); // divide money by coin
              else; 
				  if ( choice == 2 ) //if user chooses 2 for dime then
                     ( total = ( money * 100 ) / DIME ); // divide money by coin 
                    else; 
						if ( choice == 3 ) //if user chooses 3 for quarter then
	                       ( total = ( money * 100 ) / QUARTER ); //divide money by coin
	                      else; 
							  if ( choice == 4 ) //if user chooses 4 for nickel
	                             ( total = ( money * 100 ) / NICKEL ); //divide money by coin 
Last edited on
1
2
3
4
5
6
7
8
if ( money <= 1 )// if dollar amount if 1 or less end loop ... ¿what loop?
	 return -1;
		else; // <--- semicolon
			if ( choice == 1 ) 
		       ( total = ( money * 100 ) / PENNY );
              else; 
				  if ( choice == 2 ) //if user chooses 2 for dime then
                     ( total = ( money * 100 ) / DIME ); // divide money by coin  

It would work, however the else don't do anything. It will check every conditional, no matter the value of choice.
1
2
3
4
5
6
7
8
if ( money <= 1 ) // if dollar amount if 1 or less end program with an error
	return -1;
if ( choice == 1 ) //This series of conditional is not related to money but to choice, so I separate it
	total = ( money * 100 ) / PENNY;
else if ( choice == 2 )
	total = ( money * 100 ) / DIME );
//...
//here total has the value that you want  
Take a look at switch and arrays too.

I also can't test this since I cant get past the menu portion.
Then emulate the behaviour of the menu.(¿which are its effects?)
You could also make it a function int convert(int amount, int choice);
in case anyone was wondering, I figured the rest out and came up with this.

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
58
59
60
61
62
#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
{
	float money; // creates integer for money input
	int PENNY = 1; //creates integer for penny
        int DIME = 10; // creates integer for dime
	int QUARTER = 25; // creates integer for quarter
	int NICKEL = 5; // creates integer for nickel
	int total = 0; // creates integer for total and initializes it
	int choice = 0; // creates integer for choice and initializes it

	cout << "Please enter in a dollar amount to convert it to change\n"; //prompts user 
	>> cin >> money; // user enters dollar amount
	cout << endl; //end prompt
		
	cout << "To determine how many pennies, dimes, quarters or nickels, enter\n"; //display message
	cout << "MENU\n"
       << "1) PENNY\n" //user inputs 1 for penny conversion
       << "2) DIME\n" //user inputs 2 for dime conversion
       << "3) QUARTER\n" //user inputs 3 for quarter conversion
       << "4) NICKEL" << endl; //user inputs 4 for nickel conversion
	cin >> choice; //user enters number corresponding to coin
	cout << endl; //end prompt
	{
	if ( money <= 1 ) // if money input is 1 or less
	 return -1; // end program
		}
	{ // begin if...else
		if ( choice == 1 ) //if user chooses 1 for penny then
		 total = (int)( money * 100 ) / PENNY; // multiply money times 100 divided by 1 to get pennies
           else  
		     if ( choice == 2 ) //if user chooses 2 for dime then
              total = (int)( money * 100 ) / DIME; // multiply money times 100 divide by 10 to get dimes 
                else 
			     if ( choice == 3 ) //if user chooses 3 for quarter then
	               total = (int)( money * 100 ) / QUARTER; //multiply money times 100 divide by 25 for quarters
	                 else  
				       if ( choice == 4 ) //if user chooses 4 for nickel
	                     total = (int)( money * 100 ) / NICKEL; //multiply money times 100 divide by 5 for nickels
             
		
	if ( choice == 1 ) //if user choice was 1 display conversion and message
		cout << "The result is: " << total << " PENNIES in " << "$" << money << endl; // display message with amount of coins and money input
	else 
	if ( choice == 2 ) //if user choice was 2 display conversion and message
		cout << "The result is: " << total << " DIMES in " << "$" << money << endl; // display message with amount of coins and money input
	else 
	if (choice == 3 ) //if user choice was 3 display conversion and message
		cout << "The result is: " << total << " QUARTERS in " << "$" << money << endl; // display message with amount of coins and money input
	else 
	if ( choice == 4 ) //if user choice was 4 display conversion and message
		cout << "The result is: " << total << " NICKELS in " << "$" << money << endl; // display message with amount of coins and money input
} // end if..else

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