Whats wrong with my program?

For some reason it will only add the dollars correctly and does not add anything else correct. Are my functions correct?

#include <iostream>
using namespace std;

double get_coins();

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;

cout << "Enter 1 for dollars"
<< "Enter 2 for quarters"
<< "Enter 3 for dimes"
<< "Enter 4 for nickels"
<< "Enter 5 for pennies";

cin >> coins;
while(coins != 1 && coins != 2 && coins != 3 && coins != 4 && coins != 5);
{
return (coins);
}

}

double get_coins_value(int coin)
{
double coin_value;
switch(coin)
{
case 1:
coin_value = 1.00;
break;
case 2:
coin_value = .25;
break;
case 3:
coin_value = .10;
break;
case 4:
coin_value = .05;
break;
case 5:
coin_value = .01;
break;
}
return(coin_value);
}
1
2
3
4
5
cin >> coins;
while(coins != 1 && coins != 2 && coins != 3 && coins != 4 && coins != 5);
{
return (coins);
}


You're returning the coin entered, not its value.
coins will be a whole int, 1 .. 5, which is why the outer loop is only adding whole dollars.

You also have an empty while loop (note the semi-colon on the end of the while line)

Jim
Topic archived. No new replies allowed.