Why this assignment exists
You have now met the main tools of this module. You can declare variables of several types, read numbers and lines from the input, combine them with arithmetic, convert them with casts, protect a fixed rate with const, and check the results for overflow. Knowing each tool is different from using them together on a task with a clear answer. This assignment asks you to do that. It is a small point-of-sale program, the kind of program a shop runs when it prints a bill, and every line of its output depends on a decision you make about types.
Read the whole assignment before you write any code. Then write the program in small steps, compile after each step, and check the output against the examples. The rubric at the end tells you what a 60 percent, an 80 percent and a 100 percent solution look like, so you can judge your own work honestly.
The task
A small cafe prints a receipt for each customer. The cafe's point-of-sale program reads the store name and three items that the customer bought. Each item has a name, a price in cents and a quantity. The program works out the subtotal, adds the tax, and prints the receipt.
Your program must do these things, in this order:
- Read the store name as one full line, which may contain spaces.
- Read three items. Each item is one line with three values: the item name, which is one word; the price of one unit in cents; and the quantity bought.
- Work out the amount for each item, which is its price times its quantity, in cents.
- Work out the subtotal, which is the sum of the three amounts.
- Work out the tax. The tax rate is 18 percent, and it is rounded to the nearest cent, with half a cent rounding up.
- Work out the total, which is the subtotal plus the tax.
- Print the receipt, as described in the output section below.
The whole program must work in whole numbers until the last step. You will print money with a dollar sign and two digits after the point, but the calculations themselves use cents only. This is the rule from the money lesson, and the judge checks it with large inputs.
The input
The input has exactly four lines. The first line is the store name, which may contain spaces. The next three lines are the items, one per line. Each item line contains three values separated by spaces, in this order: name, price in cents, quantity.
ForgeStack Cafe
Coffee 120 2
Toast 85 1
Juice 250 3Each price is a whole number of cents from 0 to 1000000000. Each quantity is a whole number from 0 to 1000000000. The name of an item is one word, with no spaces. The store name is up to 60 characters.
The input can contain a quantity of zero. An item bought zero times still appears on the receipt, with a line amount of zero.
The output
The receipt has seven lines. Print them exactly in this form, with one space between the words and no extra spaces at the end of a line. The first line is the store name, exactly as read. The next three lines are the items. Each shows the name, then x and the quantity, then the amount in dollars and cents. The last three lines are the subtotal, the tax and the total.
ForgeStack Cafe
Coffee x2 $2.40
Toast x1 $0.85
Juice x3 $7.50
Subtotal $10.75
Tax 18% $1.94
Total $12.69Look at the amounts carefully. Two digits always follow the point, so 85 cents prints as $0.85, and 5 cents would print as $0.05. The dollar part has no padding, so 1075 cents prints as $10.75, not $010.75. A tool in the <iomanip> header, setw with setfill, pads the cents to two digits. You will use it in the reference solution in the hidden part of this page.
The tax line says Tax 18%. Print the number from your const variable, so the label and the rate can never disagree.
Examples
Here is the example from the task. Your output must match it exactly, character for character.
Input:
ForgeStack Cafe
Coffee 120 2
Toast 85 1
Juice 250 3
Output:
ForgeStack Cafe
Coffee x2 $2.40
Toast x1 $0.85
Juice x3 $7.50
Subtotal $10.75
Tax 18% $1.94
Total $12.69Work it out by hand before you run it. The coffee line is 120 times 2, which is 240 cents. The toast line is 85 times 1, which is 85 cents. The juice line is 250 times 3, which is 750 cents. The subtotal is 240 plus 85 plus 750, which is 1075 cents. The tax is 18 percent of 1075, which is 193.5 cents, and it rounds half up to 194 cents. The total is 1075 plus 194, which is 1269 cents.
Notice how the tax rounding works. The formula is (subtotal * 18 + 50) / 100, which adds 50 before the whole-number division. Half a cent is 50 on the scale of 100, so the addition moves every exact half up to the next cent. This is the same idea as the cast trick in the casting lesson, done in whole numbers so that no decimal error can creep in.
Here is a second example with a zero quantity. It checks that your program does not skip an item that was bought zero times.
Input:
Night Owl
Tea 30 0
Cake 45 0
Milk 60 4
Output:
Night Owl
Tea x0 $0.00
Cake x0 $0.00
Milk x4 $2.40
Subtotal $2.40
Tax 18% $0.43
Total $2.83Rules for this module
Use only what this module has taught. That means variables, the basic types, cin and getline, arithmetic, casts, auto, const, and the printing tools you have met. Do not use if, loops or your own functions in this assignment, because later modules teach them, and a solution that depends on them is not what this module is checking. Repeat the three item reads and the three item printing lines, since that is the honest result of the rules.
Keep one declaration per line, and give each variable a name that says what it holds. Use const for the tax rate. Do not write the number 18 or the number 50 more than once, except in the tax formula, where the 50 is the half-cent rounding and the 18 comes from the constant.
Use long long for every money value, from the line amounts to the total. The largest input in the judge's tests, a line of a billion cents times a quantity of a billion, is far beyond the range of an int. A program that uses int for money can pass the examples and then fail the judge, which is the reason for this rule.
Requirements checklist
Tick each item when you have verified it yourself. Use the Run button after each step, and compare your output with the examples.
- The program compiles with
g++ -std=c++17and no errors. - The store name is read with
getline, so a name with spaces is kept whole. - Each item is read with
cin >>, in the order name, price, quantity. - The line amount for each item is price times quantity, in
long long. - The subtotal is the sum of the three line amounts.
- The tax uses the formula
(subtotal * TAX_PERCENT + 50) / 100, withTAX_PERCENTdeclared as aconst. - The total is the subtotal plus the tax.
- Every money amount prints with a dollar sign, the dollars, a point, and exactly two digits of cents.
- A quantity of zero prints
x0and$0.00and does not break the output. - The output matches the first example exactly, including the spaces and the final newline.
- The output matches the second example exactly.
- The program handles the large test, in which a line amount is about one hundred billion cents, and prints correct values.
- The code has short comments that explain each calculation, and no unused variables.
Grading rubric
Your grade depends on what the judge sees and on what the code shows. The three levels below describe what each grade looks like in practice, so you can judge the work you have done.
| Level | What the program does |
|---|---|
| 60 percent | It compiles. It reads the store name and the three items. It prints the item lines and the subtotal correctly for the first example. It may print the tax and total incorrectly, or use int for money, which fails on large inputs. |
| 80 percent | Everything at 60 percent, plus the tax and total are correct for the first example, with the half-cent rounding. Money is printed with a dollar sign and two digits after the point. The second example, with the zero quantity, prints correctly. |
| 100 percent | Everything at 80 percent, plus long long is used for every money value, the large test prints the right dollars and cents, the tax rate is a const, the output has no extra spaces or missing newlines, and the comments make each decision clear. |
A solution that uses int for money can reach 80 percent on the small examples, but it cannot reach 100 percent, because the large test overflows. A solution that prints the tax without the half-cent rounding can be correct on the first example by luck, and it still loses the rounding marks on other inputs. Test with your own numbers, and look for the cases where a rounding step could go the wrong way.
Test your own cases
Make up three inputs of your own before you submit. Choose one with a large price, one with a quantity of zero, and one where the tax is exactly half a cent. For the half-cent case, find a subtotal where subtotal * 18 ends in 50 when divided by 100. Work out the expected answer by hand, run your program, and compare. If your answer differs, find the first line where the two disagree, and trace the values that produced it.
Hints
The hints below are hidden until you open them. Try each step on your own first, and open the hint only when you are stuck.
Hint 1: reading the store name
Use getline(cin, storeName); as the first input statement. The store name has spaces, so cin >> would stop at the first one.
Hint 2: reading the items
Each item line has a name, a price and a quantity, so read them with cin >> name >> price >> quantity;. The name is a string, and the price and quantity are long long. The reads skip the newline between lines, so you do not need cin.ignore() here, because the store name was read with getline first and the items are read with >> after it.
Hint 3: the line amount and the subtotal
The amount for an item is its price times its quantity. Store it in a long long, and add the three amounts together for the subtotal. Make sure the price is a long long before you multiply, so that the product is computed in the larger type.
Hint 4: the tax and the total
Declare const long long TAX_PERCENT = 18; at the top of the file, outside main. The tax is (subtotal * TAX_PERCENT + 50) / 100. The total is the subtotal plus the tax. Print the tax and the total with the same money pattern as the subtotal.
Hint 5: printing the money
Dollars are the cents divided by 100, and the cents are the remainder after dividing by 100. Print the dollars as they are, then a point, then the cents with setw(2) and setfill('0'), which come from the <iomanip> header. The setfill setting stays on, so it affects every later padded number, which is fine here because only money uses it.
Hint 6: the order of the output lines
The receipt prints the store name first, then the three items in the order they were read, then the subtotal, the tax and the total. Reading all four input lines before you print anything keeps the order simple.
Reference solution
The solution is shown below. Try the assignment yourself first. When you open this solution, run it with the examples in the Input box, so you see the output and can compare it with your own.
Reference solution
Run the solution with the large test below. The output should be exactly as shown, which checks the long long arithmetic and the padding together.
Input:
Vault
Gold 999999999 100
Silver 1 5
Bronze 0 1000000000
Output:
Vault
Gold x100 $999999999.00
Silver x5 $0.05
Bronze x1000000000 $0.00
Subtotal $999999999.05
Tax 18% $179999999.83
Total $1179999998.88Notice that the tax in the large test is the half-cent formula at work on a big number. The subtotal in cents is 99999999905, and 18 percent of that is 17999999982.9, which rounds up to 17999999983 cents. A program that used int for the subtotal would already have wrapped by the time it reached the tax.
What this module taught
The assignment brings the whole module together. Here is the list of what it used, and the place in the module where each tool was introduced.
- A program starts at
main, and it is compiled withg++ -std=c++17into an executable that prints the output exactly as the judge expects. - Variables have a type, a name and a value. You declare them, initialise them and change them by assignment.
- The basic types are
intandlong longfor whole numbers,doublefor decimals,charfor one character,boolfor true or false, andstringfor text. cin >>reads values and skips spaces, andgetlinereads a whole line. A leftover newline can trap a latergetline.- A cast,
auto,constand the limits of each type decide whether a calculation is correct, and money belongs in whole cents. - Compiler messages are read from the top, one at a time, and a judge compares your output exactly.