This section introduces fundamental concepts in \verb|C++|, focusing on input/output operations and arithmetic computations.
The retail establishment under consideration offers a diverse array of items. To facilitate transactions, it is imperative to adeptly handle input, output, arithmetic operations. Specifically, the task involves computing the subtotal, tax, and total for items purchased by customers. The subsequent presentation of this information to the customer is essential for transparency regarding the payment amount. Furthermore, the system must handle payment an calculate change due. This section is dedicated to the development of a program that accomplishes these tasks seamlessly.
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.
The initial step involves gathering information about a single product that a customer intends to purchase. Three crucial pieces of information are required for the chosen item: the name of the product, the cost of the product, and the desired quantity. To obtain these details, a sequential approach is employed.
To capture the name of the product, the user is prompted through a cout statement, followed by utilizing getline for reading in the name. This choice is made because the name, being a string, might include spaces, and cin is unable to handle spaces. Subsequently, a similar process is applied to obtain the cost, where the user is prompted with a cout statement, and the cost is read in using a cin statement. The same procedure is replicated to capture the qty.
Three distinct variables of varying data types have been pre-declared in the provided program to store the acquired information. These variables are summarized in the table below:
| Data Type | Identifier | Description |
|---|---|---|
string | name | Name of the item |
double | cost | Cost of the item |
int | qty | Quantity wanted of the item |
Item Name: Example Item Cost: 5.99 Quantity: 1
Data types: www.youtube.com/watch?v=KN78kXrdqxA
Variables: www.youtube.com/watch?v=B27K3OB9fVA
Input/output: www.youtube.com/watch?v=42n9YyKYJMo
Having acquired the cost and the desired qty of the item, we can now proceed to calculate three essential pieces of data: the subtotal, the tax charge, and the total price. These calculations involve a sequential process utilizing arithmetic operations.
Three variables, already declared in the provided program, will be used to store these computed values. They are summarized below:
| Data Type | Identifier | Description |
|---|---|---|
double | subtotal | Subtotal for the specified quantity of the item |
double | tax | Tax to be charged for the specified quantity of the item |
double | total | Total price, including tax, for the specified quantity |
To calculate the subtotal, the following formula is applied:
subtotal = cost * qty;
Once the subtotal is obtained, the tax can be calculated using the Nevada sales tax rate of 8.25%. A global constant TAX_RATE is provided in the program. The formula is:
tax = subtotal * TAX_RATE;
Note: For this assignment, rounding of the
taxto the nearest hundredth is not performed. More advanced calculations using libraries will be introduced in future assignments.
Finally, calculate the total by incorporating the tax:
total = subtotal + tax;
The subtotal, tax, and total are then presented to the customer using cout statements, providing a clear summary of the final amount due.
Item Name: Example Item Cost: 5.99 Quantity: 1 Subtotal: 5.99 Tax: 0.494175 Total: 6.48418
With the total amount displayed for the customer, we can proceed to receive the payment and incorporate the payment details into our program. Employing a methodology similar to Section 1.1, cout statements prompt for the tendered amount, followed by cin statements to capture the input. Subsequently, the change owed to the customer is calculated using the total and tendered amounts.
The variables designated for these amounts have already been declared in the provided program. These variables are summarized in the table below:
| Data Type | Identifier | Description |
|---|---|---|
double | tendered | Payment amount from the customer |
double | change | Change owed to the customer |
Once the tendered amount is obtained, the change owed to the customer can be calculated using the formula:
change = tendered - total;
Item Name: Example Item Cost: 5.99 Quantity: 1 Subtotal: 5.99 Tax: 0.494175 Total: 6.48418 Amount tendered from customer: 10 Change Due: 3.51582
Below are sample executions of the complete program. Text in red is input from the user; this coloring is simply here to distinguish components and is not needed in your program.
Note: The line breaks after the inputs in the example output below are the default ones C++'s cin and getline() automatically apply after reading input from the user. CodeGrade supplies input differently (via Linux redirection), so its auto tests will not show line breaks after inputs. Match these examples and it should align correctly in CodeGrade.
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.494175 Total: 6.48418 Amount tendered from customer: 10 Change Due: 3.51582
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: Laptop Item Cost: 799.99 Quantity: 3 Subtotal: 2399.97 Tax: 197.998 Total: 2597.97 Amount tendered from customer: 2600 Change Due: 2.03247
main.cpp. Do not ignore this step or save your file(s) with different names..cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.