This thing is kicking my ass. I've been through my book, and I have even downloaded the professor's notes on subject (he posted them). I can't find what I'm doing wrong. Below is the code I've written, below the code is what the console is "supposed" to look like.
int main()
{
int price, amountpaid;
float change;
cout << "Please give the price of the item (example: $5.95).\n";
cin >> price;
while (price > 0.00)
{
cout << "Please enter the amount you wish to pay\n";
cin >> amountpaid;
if (amountpaid <=0)
{
cerr << "Invalid input, program closing\n";
exit (1);
}
if (amountpaid < price)
{
cout << " You still owe " << price - amountpaid << " dollars ";
}
if (amountpaid >=0)
change= amountpaid-price;
cout << " You will recieve " << change << " dollars in change." << endl;
exit (1);
}
return 0;
}
(Code ends here)
For some reason I cannot put any number with a decimal in for "price" or "amountpaid". Also, The change is supposed to show up as WHAT change is being recieved, example:
1 Ten
2 Fives
0 Ones
2 Quarters
0 Dimes
0 Nickels
1 Penny
No matter what I've tried to do, I can't get farther than the above code. Can anyone help me?
int price, amountpaid;
That means that the price and the amountpaid are integral type which means that they can't handle decimal points, so even if you enter them they just drop them. Change it to double price, amountpaid;
To find the exact change to divisions of your number with each amount (ten, fives etc) and use the modulo % operator to calculate them:
Example: 5.95 / 10 = 0 tens (this must be int) and remaining 5.95
5.95 / 5 = 1 five and remaining .95
etc...
Thanks for the reply Mitsakos. How do you use this "modulo %" operator? I've never heard of it, and I can't find anything on it in my notes. How exactly would it fit into the current code? Do I need to write another "if" statement? Thanks for any help you can offer!
First of all change % 20 = Twenty; is invalid Twenty = change % 20; whould be better.
Then it is better to multiply everything with 100 so you can have better results for the cents.
For example:
1 2 3 4 5
double num = 6.95;//Your number
int remaining = num * 100;//multiply with 100 for ease of calculations
cout << "20's: " << remaining/2000 << '\n';//the integer of the value divided with 2000
//(20 dollars) is how many 20's you need
remaining = remaining % 2000;//now you have the rest of your amount
All the whole dollars work. What doesn't work however is the change (Quarters, Dimes, nickels, pennies). What did I do wrong here? So far, it seems like only the pennies and nickels are not working. Putting in 5.95 for Price, and 6.00 for amountpaid produces 1 Nickel and 5 Pennies. I can't figure out how to get 1 nickel 0 pennies.
You made a copy-paste mistake....
Look your lines 9, 13 and 15...
Every time you go down you should change the value of the modulo, but you have them %100... Which is wrong...
line 15 for example should be Dime = change % 50 /10;