Mar 20, 2013 at 4:29pm UTC
Whats wrong with this?
#include <iostream>
using namespace std;
double get_coins();
double get_coins_value();
int main()
{
const double price = 3.50;
double total_so_far = 0.0;
cout << "Please Insert $3.50 to recieve your twinkie" << endl;
do
{
total_so_far += get_coins();
}
while(total_so_far < price);
if (total_so_far > price)
{
cout << "Your change is " << total_so_far - price << endl;
}
return 0;
}
double get_coins()
{
int coins;
double total = 0.0;
cout << "Enter 1 for dollars "
<< "Enter 2 for quarters "
<< "Enter 3 for dimes "
<< "Enter 4 for nickels ";
cin >> coins;
total += get_coins_value();
while(coins != 1 && coins != 2 && coins != 3 && coins != 4);
{
return (total);
}
}
double get_coins_value(int coins)
{
double coin_value;
switch(coins)
{
case 1:
coin_value = 1.00;
break;
case 2:
coin_value = 0.25;
break;
case 3:
coin_value = .10;
break;
case 4:
coin_value = .05;
break;
}
return(coin_value);
}
Mar 20, 2013 at 4:33pm UTC
What makes you think there is something wrong with your code?
Mar 20, 2013 at 4:38pm UTC
1) your get_coins_value prototype is wrong, should be: double get_coins_value(int );
2) total += get_coins_value();
should be total += get_coins_value(coins);
Mar 20, 2013 at 4:50pm UTC
You missing semicolon at the end of the 1st line. It can be written as:
1 2 3 4
while (coins != 1 && coins != 2 && coins != 3 && coins != 4)
;
//or
while (coins != 1 && coins != 2 && coins != 3 && coins != 4) { };
This loop will be infinite (hang program) if you enter anything aside from 1, 2, 3 or 4.
Just delete that line. It makes no sense.
Last edited on Mar 20, 2013 at 4:51pm UTC