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;
"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.
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.
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.
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.
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
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;
elseif ( 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);
#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
elseif ( choice == 2 ) //if user chooses 2 for dime then
total = (int)( money * 100 ) / DIME; // multiply money times 100 divide by 10 to get dimes
elseif ( choice == 3 ) //if user chooses 3 for quarter then
total = (int)( money * 100 ) / QUARTER; //multiply money times 100 divide by 25 for quarters
elseif ( 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
elseif ( 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
elseif (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
elseif ( 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
}