This thing is pushing me around. 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?

Note: I've understood that somehow, "modulo" operators are needed here. But I haven't the foggiest idea how to use them! Can anyone, with great detail, tell me how I could use them here?

Recommended Answers

All 7 Replies

Member Avatar for iamthwee

> For some reason I cannot put any number with a decimal in for "price" or "amountpaid".
You need to declare the type double > But I haven't the foggiest idea how to use them!
Google it, but for your information the percentage sign is the modulus operator.

I know that the "%" sign is the modulus operator. The problem is, HOW to use it to get the code I'm looking for?

Is it:

(change % 20 == 0)?
I haven't the slightest idea. I've goolged "how to use modulo operators", and all I've gotten is the basic: "11 % 3 = 2" (2 being a remainder).

Member Avatar for iamthwee

Plan it out on paper or draw a flow chart if necessary.

Member Avatar for iamthwee

>How will that help?

Well a program is just a flow chart really, that's why.

>How will that help?

Well a program is just a flow chart really, that's why.

I know what I'm after, I don't know WHAT or HOW.

What I've been typing:

change = Penny % 100;
change = Nickel % 20;
change = Dime % 10;
change = Quarter % 4;

and so on

That doesn't work. I don't know how to use modulos operators in the sense that I want the remainders to be used later so that I can get the full amount of money. All the guides I've found from google just use modulos "%" once in the program, I need to use it eight times!

If you are going to use the modulo operator, convert everything to cents first. So, say you are trying to figure out how many ten dollar bills are in $38.45. Ten dollars is 1000 cents. $38.45 is 3845 cents. You use the / operator to calculate the number of tens:

int numTens = 3845 / 1000;

Result? numTens will equal 3.

Now for the rest of your change, after you give out the three tens, which is worth $30.00, you'll have $38.45 - $30.00 = $8.45 remaining, or 845 cents. To get THAT, you use the % operator:

int numCentsRemaining = 3845 % 1000;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.