Hello, I just started programming a few days ago so it would be a big help if the explanations are simple for me to understand. So for my assignment I need to make a cash register program where you input how much the cost was, then you input how much money you paid, and then the program will spit out what your change value is and what bills you'll get.
Say the cost is $3.54 and the payment is $40.
My problem is that for the quarter, dime, nickel,and penny calculation, it says "invalid operands of type 'int' and 'double'" and I'm not seeing where the problem is.
Your using the % function incorrectly. It only works on integers.
What this means is you should calculate the change in cents (integers)
So if you have change of 5.23 then you calculate with an integer vale of 523 cents.
The number of quarters is 523 / 25 and the left over cents are 523 % 25, and that leftover amount is then to be broken down further. You don't need to keep adding up the various numx values.
//Making Change ($)
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
double purchase;
double payment;
double change;
int twenty;
int ten;
int five;
int one;
int quarter;
int dime;
int nickel;
int penny;
do
{
cout << "Please enter the value of your purchase. (0-100)" << endl;
cin >> purchase;
}
while (purchase < 0 || purchase > 100);
do
{
cout << "Please enter the value of how much you paid." << endl;
cin >> payment;
}
while (payment <= purchase);
cout << "Here is your change:";
change = payment - purchase;
cout << fixed << setprecision(2) << change << endl;
int cents = (int)100*change;
twenty = cents/2000;
cents = cents % 2000;
ten = cents/1000;
cents = cents % 1000;
five = cents/500;
cents = cents % 500;
/* and so on */
cout << "Twenties: " << twenty << endl;
cout << "Tens: " << ten << endl;
cout << "Fives: " << five << endl;
cout << "Ones: " << one << endl;
cout << "Quarters: " << quarter << endl;
cout << "Dimes: " << dime << endl;
cout << "Nickels: " << nickel << endl;
cout << "Pennies: " << penny << endl;
return 0;
}
Please enter the value of your purchase. (0-100)
2.15
Please enter the value of how much you paid.
50
Here is your change:47.85
Twenties: 2
Tens: 0
Fives: 1
Ones: 0
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0
Program ended with exit code: 0
This is odd. So I've been using Qt to run and compile things and the program output in Qt isn't the same as the browser's. In the browser, it shows that everything is correct. The bills and cents have the proper output in the browser shell. However, Qt shows the proper values for the bills but not the cents; the cents are all 0.
Qt output:
Please enter the value of your purchase. (0-100)
3.45
Please enter the value of how much you paid.
50
Here is your change:46.55
Twenties: 2
Tens: 0
Fives: 1
Ones: 1
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0
Output from the browser shell:
Please enter the value of your purchase. (0-100)
3.45
Please enter the value of how much you paid.
50
Here is your change:46.55
Twenties: 2
Tens: 0
Fives: 1
Ones: 1
Quarters: 2
Dimes: 0
Nickels: 1
Pennies: 5
Please enter the value of your purchase. (0-100)
1.23
Please enter the value of how much you paid.
50.00
Here is your change:48.77
Twenties: 2
Tens: 0
Fives: 1
Ones: 3
Quarters: 3
Dimes: 0
Nickels: 0
Pennies: 2
Exit code: 0 (normal program termination)
This is what you get by running the shell here (gear wheel on the right of your code box above) and the answer looks =good to me.
Try clean and rebuild in Qt because the answer will be the same.
Lines 60 and 63, are incorrect, giving wrong answers. For the dime, you are still using mod for a quarter, instead of for 10, and for nickels, you are using mod for a dime. Correct those two, and you should get much better results.
Thanks whitenite1. Two heads are better than one, especially when one of them is mine and it wasn't working at the time. I couldn't have picked a worse test example.
@kemort and @whitenite1 Thank you both for helping me out. @kemort I actually just created a new project in Qt and that seemed to solve the problem. I tried to clean and rebuild just as you said, but it didn't seem to do anything. I'll go and look up how to do it properly another time.
Here is my final code. I fixed the calculation part and added a few more statements. Also, if the bills showed a value of 0, I made it so it wouldn't output.
//Making Change ($)
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
double cost;
double payment;
double change;
int twenty;
int ten;
int five;
int one;
int quarter;
int dime;
int nickel;
int penny;
cout << "Please enter the cost of your purchase. (0-100)" << endl;
cin >> cost;
while (cost <= 0 || cost >= 100) {
cout << "Please enter a cost value from 0 to 100." << endl;
cin >> cost;
}
cout << "Please enter how much you paid for the purchase." << endl;
cin >> payment;
while(cost > payment) {
cout << "Please enter a payment value higher than your cost value." << endl;
cin >> payment;
}
cout << "Here is your total change: " << endl;
change = payment - cost;
cout << fixed << setprecision(2) << change << endl;
int total = (int)100*change;
twenty = total/2000;
total = total % 2000;
ten = total/1000;
total = total % 1000;
five = total/500;
total = total % 500;
one = total/100;
total = total % 100;
quarter = total/25;
total = total % 25;
dime = total/10;
total = total % 10;
nickel = total/5;
total = total % 5;
penny = total;
cout << "Here is your change in dollar bills: " << endl;
if (twenty != 0) {
cout << "Twenties: " << twenty << endl;
}
if (ten != 0) {
cout << "Tens: " << ten << endl;
}
if (five != 0) {
cout << "Fives: " << five << endl;
}
if (one != 0) {
cout << "Ones: " << one << endl;
}
if (quarter != 0) {
cout << "Quarters: " << quarter << endl;
}
if (dime != 0) {
cout << "Dimes: " << dime << endl;
}
if (nickel != 0) {
cout << "Nickels: " << nickel << endl;
}
if (penny != 0) {
cout << "Pennies: " << penny << endl;
}
return 0;
}
I tried to clean and rebuild just as you said, but it didn't seem to do anything.
It wouldn't have done any good given my blooper in not looking at your code but relying on an incomplete test case which gave gave the correct result of zero for the wrong reasons.
Well after I fixed the calculation and ran it, the output still showed 0s for the cents. I searched on Google what to do when in this scenario and one person said you could just make a new project. I made a new project, copy and pasted, and then the output was corrected.