Help writing simple code with dollars PLEASE

Please help me complete the code for this task! Thank you in advance!
Write a program that reads an amount of dollars int amount. Then the program determines how many $20 bills, $10 bills, $5 dollar bills and $1 dollar bills are needed to represent the amount. Use four more variables int twenties, tens, fives, singles; for which you are to compute their respective values using integer division and remainder (operators / and % respectively.)For instance: say amount = 137. Then we need
137 / 20 = 6 twenties; the remainder 137 % 20 = 17.
17 / 10 = 1 tens; the remainder 17 % 10 = 7
7 / 5 = 1 fives; the remainder 7 % 5 = 2;
At this point the last remainder is 2 so that we need 2 singles.

The program should print the following line
For the amount of $137 we need 6 twenties, 1 tens, 1 fives, and 2 singles.
Well, by examining the example you will already solve most of the problem

first try make the code then tell us where you have problems
Last edited on
Robin:Hop digity Batman! It's another person trying to get people to do their homework for them! What shall I do?
Batman: Give them a start.

Here you go. Just for you :D
1
2
3
4
5
6
7
int main()
{


return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {

int amount;
cout << "Enter amount of dollars. ";
cin >> amount;
int twenties, tens, fives, singles;
amount = 137;
137 / 20 = 6 twenties; the remainder 137 % 20 = 17;
17 / 10 = 1 tens; the remainder 17 % 10 = 7;
7 / 5 = 1 fives; the remainder amount 7 % 5 = 2;
cout << "For the amount of ">> amount >> "we need ">> twenties, tens, fives, singles;

return 0;
}

can you please help me fix this? thanks a bunch
You need to learn about variables, data types, and operators. The "Basics of C++" section in the C++ Language Tutorial on this site covers that. I recommend you read through that section. Here is the link: http://www.cplusplus.com/doc/tutorial/

To give you an idea, you will need to assign the results of integer division to the variables you've defined. For example, to get the number of twenties in amount, you can do this:

twenties = amount / 20;

You can use the modulus operator to get the remainder of amount divided by 20 in order to calculate the number of tens, and so on. This should replace lines 11 through 13 in your code.

Also, since you are asking the user to enter the amount at the console via the cin statement, you don't want to assign 137 to amount as you are doing in line 10.
Last edited on
Thank you for replying. I still have problems on line 14 though and am unsure how to fix it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {

        int amount;
        cout << "Enter amount of dollars. ";
        cin >> amount;
        int twenties, tens, fives, singles;
        twenties = amount / 20; twenties % 20;
        tens = amount / 10; tens % 10;
        fives = amount / 5; fives % 5;
        singles = amount / 1; singles % 1;
        cout << "For the amount of ">> "$" >> amount >> ", we need " >> twenties, tens, fives, singles;

        return 0;
}
closed account (jwkNwA7f)
cout << "For the amount of ">> "$" >> amount >> ", we need " >> twenties, tens, fives, singles;

Should be:
cout << "For the amount of " << "$" << amount << ", we need " << twenties << tens << fives << singles;
Last edited on
Please read about the assignment operator (the equal sign) in the tutorial.

This is an assignment statement that stores the integer quotient of amount divided by 20:

twenties = amount / 20;

The above is correct. But this next statement doesn't assign anything, and it is incorrect:

twenties % 20;

Even if it was an assignment statement, you aren't dealing with the right values there. Think about how you are approaching it. The modulus operator gets the remainder of integer division. The value that was stored in variable twenties by the previous assignment statement is the number of 20's in amount. You don't need the remainder of that divided by 20. It is the remainder of amount divided by 20 that you need in order to get the number of 10's.

You can reuse the amount variable to hold the remainder before extracting the next monetary type.

I hesitate to say more than this because I believe you can figure it out if you would review the section in the tutorial on assignment statements and the division and modulus operators.
Last edited on
Thanks again for your replies. I'm sorry I've been struggling with this code for the past 3 hours and I still don't understand even after reviewing the tutorial. Please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main() {

int amount;
cout << "Enter amount of dollars. ";
cin >> amount;
int twenties; tens; fives; singles;
int remainder;
twenties = amount / 20; amount % 20 = remainder;
tens = remainder / 10; amount % 10 = remainder;
fives = remainder / 5; amount % 5 = remainder;
singles = remainder / 1;
cout << "For the amount of " << "$" << amount << ", we need " << twenties << "twenties, " << tens << "tens, " << fives << "fives, and " << singles << "singles.";

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main() {

int input,amount,twenties,tens,fives,singles;
cout << "Enter amount of dollars. ";
cin >> amount;
input=amount;
twenties = amount / 20;
amount %= 20;
tens = amount / 10;
amount %= 10;
fives = amount / 5;
amount %= 5;
singles = amount / 1;
cout <<"For the amount of $"<<input<<" we need "<<twenties<<" twenties, "
<<tens<<" tens, "<<fives << " fives, and " << singles << " singles."<<endl;

return 0;
}
Thank you Chrisscpp, it finally works and I understand where I went wrong!!!!!
Topic archived. No new replies allowed.