code compiles correctly but won't run correctly

Hello,
I am working on an assignment to convert dollars to coins. I have the code written and it compiles with out any errors. My problem is that there is a section where the user is to choose between 4 numbers and then convert the dollar amount to the chosen type of coin. The program does not give the user time to enter the choice and continues to run my final message. Any help would be appreciated! This is what I have so far:

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

int main()// function main begins program execution
{
int money; // creats integer for money input
const int PENNY = 1; // creates constant integer for penny
const int NICKEL = 5; // creates consttant integer for quarter
const int DIME = 10; // creates constant integer for dime
const int QUARTER = 25; // creates constant integer for quarter
int total = 0;

cout<< "Enter dollar amount you wish to convert to coins\n"; //prompt user to input
cin >> money; // user enters dollar amount

cout << "Choose the coin you wish to have the amount displayed in from the menu\n"; // display message
cout << "MENU\n"
<< "Enter 1 for Pennies\n"
<< "Enter 2 for Nickels\n"
<< "Enter 3 for Dimes\n"
<< "Enter 4 for Quarters\n" << endl;

char choice;
cin >> choice;

{
if ( money < 1 ) // if dollar amount is 1 or less end loop
return -1;
else if ( choice == '1' ) // if user enters penny
total = ( money * 100) / PENNY; // divide money by coin
else if ( choice == '2' ) // if user enters nickel
total = ( money * 100) / NICKEL; // divide money by coin
else if ( choice == '3') // if user enters dime
total = ( money * 100) / DIME; // divide money by coin
else if ( choice == '4') // if user enters quarter
total = ( money * 100 ) / QUARTER; // divide money by coin
}

cout << "Your change amount is: " << total << " in" << choice << endl; // display message with conversion

system("pause");
return 0; // indicates successful termination
}
closed account (o3hC5Di1)
Hi there,

Strange - your program runs correctly here, compiled with GCC.
Which IDE / Compiler are you using?

Perhaps this is the issue:

1
2
3
4
5
6
7
8
9
10
11
12
{  //why are there curly braces here?
if ( money < 1 ) // if dollar amount is 1 or less end loop
return -1;
else if ( choice == '1' ) // if user enters penny
total = ( money * 100) / PENNY; // divide money by coin
else if ( choice == '2' ) // if user enters nickel
total = ( money * 100) / NICKEL; // divide money by coin
else if ( choice == '3') // if user enters dime
total = ( money * 100) / DIME; // divide money by coin
else if ( choice == '4') // if user enters quarter
total = ( money * 100 ) / QUARTER; // divide money by coin
} //and here/ 


Remove those curly braces and try again.

On a sidenote:

- you can make "choice" an int, as you're expecting a number
- replacing the elfe if() statements by a switch() construct woul be recommended in this case, for more info see http://cplusplus.com/doc/tutorial/control/#switch

All the best,
NwN
Topic archived. No new replies allowed.