Writing a simple calculation program.

Sep 14, 2008 at 6:11am
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.

(Code begins here):
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

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?
Sep 14, 2008 at 6:33am
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...
Last edited on Sep 14, 2008 at 6:34am
Sep 14, 2008 at 2:33pm
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!

I take it that THIS isn't what I needed to do:

}
if (amountpaid >=0)
change = amountpaid-price;
change % 20 = Twenty;
change % 10 = Ten;
change % 5 = Five;
change % 1 = One;
change % .25 = Quarter;
change % .10 = Dime;
change % .05 = Nickel;
change % .01 = Penny;
cout << " You will recieve " << change << " dollars in change." << endl;
exit (1);
}
return 0;
}

I'm getting an error: Error 2 error C2296: '%' : illegal, left operand has type 'float

Whats this mean? I can't float change?
Last edited on Sep 14, 2008 at 3:55pm
Sep 14, 2008 at 5:10pm
"change" has to be int becouse modulo doesnt work with floats
1
2
3
4
5
6
7
if (amountpaid >=0){
change = (amountpaid-price)*100;
Twenty=change/2000;
Ten=change%2000/1000;
Five=change%1000/500;
...
}
Sep 14, 2008 at 5:20pm
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 


Hope this helps

[edit] Too late...
Last edited on Sep 14, 2008 at 5:22pm
Sep 14, 2008 at 7:40pm
First off I'd like to thank hamsterman and Mitsakos for getting me this far. After putting it in, I finally got the whole "modulos" thing.

Well, sort of. Here is the code I put in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    }
    if (amountpaid >=0)
 change = (amountpaid - price)*100; 
    {
Twenty = change/2000;
cout << Twenty << " Twenty\n";
Ten = change%2000/1000;
cout << Ten << " Ten\n";
Five = change%1000/500;
cout << Five << " Five\n";
One = change%500/100;
cout << One << " One\n";
Quarter = change%100/25;
cout << Quarter << " Quarters\n";
Dime = change %100/10;
cout << Dime << " Dime\n";
Nickel = change%100/5;
cout << Nickel << " Nickel\n";
Penny = change%100/1;
cout << Penny << " Penny\n" << endl; 

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.
Sep 15, 2008 at 8:32am
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;

hope this helps
Topic archived. No new replies allowed.