C++ Assignment: The UNLV CS Shop Part 2

Thu Aug 14 2025
Updated: Fri Aug 15 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

This section introduces fundamental concepts in C++, focusing on utilization of the <cmath> library, and integration of the <iomanip> library.


The preceding assignment concluded with the computation of values such as subtotal, tax, total, and change due. However, these numerical values have the potential to extend well beyond the hundredths place, contrary to the standard representation of money that typically extends only up to 99 cents. To address this discrepancy, we will now introduce rounding techniques, leveraging the <cmath> library.

The implementation of rounding will rectify the precision of these monetary values, ensuring that they adhere to the conventional representation format. Once this refinement is applied, the subsequent step involves generating a comprehensive receipt for the customer's transaction. The creation of a receipt will be simulated, incorporating functionalities from the <iomanip> library.

To actively engage with this section, it is recommended to download the provided program main.cpp, which serves as the foundation for coding exercises and practical application of the concepts discussed.


Rounding

Recall the output from the previous assignment, which resembled the following:

... Subtotal: 5.99 Tax: 0.494175 Total: 6.48418 ... Change Due: 3.51582

The issue arises when the calculated tax extends beyond the hundredths place, causing discrepancies in both the total and change due. This occurs because the trailing tax, when added to the subtotal, introduces trailing decimals. Consequently, subtracting this trailing total from the tendered amount results in a change due with trailing decimals. The solution lies in fixing the tax calculation, and the rest of the computations will follow suit.

Recall how the tax was previously calculated:

tax=subtotalTAX_RATEtax = subtotal * TAX\_RATE

Instead of a straightforward addition, we introduce the following equations:

multiplier=pow(10,numDecimalPlaces)multiplier = pow(10, numDecimalPlaces) tax=subtotalimesTAX_RATEtax = subtotal imes TAX\_RATE roundedTax=ceil(taximesmultiplier)÷multiplierroundedTax = ceil(tax imes multiplier) \div multiplier

For this assignment, always use the value 2 for numDecimalPlaces when calculating the multiplier. The tax should be computed in the same manner as in the previous assignment. Subsequently, the multiplier and tax values are utilized in the final equation to calculate the tax rounded to two decimal places. The roundedTax can then replace all occurrences of tax throughout the rest of the program. Note: This rounding technique will always round up.

After adding these changes the output should resemble the following:

... Subtotal: 5.99 Tax: 0.5 Total: 6.49 ... Change Due: 3.51

The tax is only showing a single decimal place though since everything past the tenths place is 0. To show the 0, use a fixed precision of 2 using setprecision() (reference).

After adding this change the output should resemble the following:

... Subtotal: 5.99 Tax: 0.50 Total: 6.49 ... Change Due: 3.51

For an introduction to the <cmath> library, consult this video: YouTube


Output Manipulation

After calculating all the necessary values, the next step is to present them on a well-formatted receipt. The provided program contains the initial structure for the receipt under the comment // 2.2: Print the receipt.

To achieve proper formatting resembling a receipt, utilize the <iomanip> library. Employ the following functions:

  • setw()): Set field widths for outputs.

  • setfill()): Specify the character output in the white space of the buffer provided by setw().

  • left) and right): Justify output to the left and right within the setw() buffers.

For the item details (name, cost, qty), follow this format:

  1. Output the string "| "

  2. Justify the output to the left, set a field width of 26, and display the name.

  3. Justify the output to the right, set a field width of 6, and present the cost.

  4. Output the string " x " followed by the qty and " |\n".

  5. Introduce a blank line using the same code as the one used after the phone number in the receipt.

Proceed to output:

  • Subtotal: Output "| Subtotal ", set a field width of 27, and display the subtotal followed by " |\n".

  • Tax Rate: Output "| Tax Rate ", set a field width of 26, , and display the Tax_Rate * 100 followed by "% |\n".

  • Tax: Output "| Tax ", set a field width of 32, and display the tax followed by " |\n".

  • Total: Output "| Total ", set a field width of 30, and display the total followed by " |\n".

  • Introduce a blank line using the same code as the one used after the phone number in the receipt.

  • Tendered amount: Output "| Tendered ", set a field width of 27, and display the tendered amount followed by " |\n".

  • Change due: Output "| Change ", set a field width of 29, and display the change due followed by " |\n".

  • Introduce a blank line using the same code as the one used after the phone number in the receipt.

The program already includes the code to print the bottom of the receipt.

The output receipt should look similar to the following (with differing numbers/item names depending on the values input):

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ | | | UNLV CS Shop | | 4505 S Maryland Pkwy | | Las Vegas,NV 89154 | | (702) 895-3011 | | | | Example 5.99 x 1 | | | | Subtotal 5.99 | | Tax Rate 8.25% | | Tax 0.50 | | Total 6.49 | | | | Tendered 10.00 | | Change 3.51 | | | \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

For help with the <iomanip> library: YouTube


Example Program Interaction

Alexs-iMac desktop % g++ main.cpp Alexs-iMac desktop % ./a.out +-----------------------------------------------------------------------------+ | UU UU NNNN NN LL VV VV CCCCCC SSSSSSSS | | /UU /UU /NN/NN /NN /LL /VV /VV CC////CC SS////// | | /UU /UU /NN//NN /NN /LL /VV /VV CC // /SS | | /UU /UU /NN //NN /NN /LL //VV VV /CC /SSSSSSSSS | | /UU /UU /NN //NN/NN /LL //VV VV /CC ////////SS | | /UU /UU /NN //NNNN /LL //VVVV //CC CC /SS | | //UUUUUUU /NN //NNN /LLLLLLLL //VV //CCCCCC SSSSSSSS | | /////// // /// //////// // ////// //////// | | | | SSSSSSSS HH HH OOOOOOO PPPPPPP | | SS////// /HH /HH OO/////OO /PP////PP | | /SS /HH /HH OO //OO /PP /PP | | /SSSSSSSSS /HHHHHHHHHH /OO /OO /PPPPPPP | | ////////SS /HH//////HH /OO /OO /PP//// | | /SS /HH /HH //OO OO /PP | | SSSSSSSS /HH /HH //OOOOOOO /PP | | //////// // // /////// // | +-----------------------------------------------------------------------------+ Item Name: Example Item Cost: 5.99 Quantity: 1 Subtotal: 5.99 Tax: 0.50 Total: 6.49 Amount tendered from customer: 10 Change Due: 3.51 /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ | | | UNLV CS Shop | | 4505 S Maryland Pkwy | | Las Vegas,NV 89154 | | (702) 895-3011 | | | | Example 5.99 x 1 | | | | Subtotal 5.99 | | Tax Rate 8.25% | | Tax 0.50 | | Total 6.49 | | | | Tendered 10.00 | | Change 3.51 | | | \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Note: The line breaks after the inputs in the example output formatting below are the default ones C++'s cin and getline() automatically apply after reading an input from the user. CodeGrade supplies input in a different manner (Linux redirection) to programs so in CodeGrade's auto tests there will not be line breaks after the inputs in CodeGrade. Match the pdf and it should match in CodeGrade.


Hand-In Procedure

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.