/**************************************************************** File: cashiercalculator.cpp Description: This program calculates the cost for each of the item, sales tax, and the total price before and after tax. All based on what the user inputs then it displays a sumamry of the purchase and calculates the amount of change to be rendered to the customer. Author: Ray Mansour Class: Introduction to Computer Science I (c++) - 110-71 Date: 10/6/2023 I hereby certify that this program is entirely my own work. *****************************************************************/ #include <iostream> #include <iomanip> using namespace std; int main() { cout << "Ray's Awesome Store Cashier Program" << endl; const int denominations[] = {100, 20, 10, 5, 1, 25, 10, 5, 1}; const string denominations_names[] = {"$100 bills", "$20 bills","$10 bills","$5 bills", "$1 bills", "quarters", "dimes", "nickels","pennies"}; const int num_denominations = 9; string item_names[3]; double unit_price[3]; int quantity[3]; double total_cost_before_tax = 0.0; double tax_rate; double tax; double total_cost_after_tax; double payment_amount; double change; cout << "Enter details for item 1:" << endl; cout << "Item name: "; cin.ignore(); cin >> item_names[0]; cout << "Unit price ($): "; cin >> unit_price[0]; cout << "Quantity "; cin >> quantity[0]; double item_cost_1 = unit_price[0] * quantity[0]; total_cost_before_tax += item_cost_1; cout << endl; cout << "Enter details for item 2:" << endl; cout << "Item name: "; cin.ignore(); cin >> item_names[1]; cout << "Unit price ($): "; cin >> unit_price[1]; cout << "Quantity: "; cin >> quantity[1]; double item_cost_2 = unit_price[1] * quantity[1]; total_cost_before_tax += item_cost_2; cout << endl; cout << "Enter details for item 3:" << endl; cout << "Item name: "; cin.ignore(); cin >> item_names[2]; cout << "Unit price ($): "; cin >> unit_price[2]; cout << "Quantity: "; cin >> quantity[2]; double item_cost_3 = unit_price[2] * quantity[2]; total_cost_before_tax += item_cost_3; cout << endl; do { cout << "Enter the sales tax rate (%): "; cin >> tax_rate; if (tax_rate < 0 || tax_rate > 100) { cout << "Please enter a valid tax rate between 0 and 100." << endl; } else { break; // valid input } } while (true); tax = total_cost_before_tax * (tax_rate / 100); total_cost_after_tax = total_cost_before_tax + tax; cout << fixed << setprecision(2); cout << setw(20) << "Item Name" << setw(20) << "Unit Price ($)" << setw(10) << "Quantity" << setw(15) << "Cost ($) " << endl; cout << setw(20) << item_names[0] << setw(20) << unit_price[0] << setw(10) << quantity[0] << setw(15) << unit_price[0] * quantity[0] << endl; cout << setw(20) << item_names[1] << setw(20) << unit_price[1] << setw(10) << quantity[1] << setw(15) << unit_price[1] * quantity[1] << endl; cout << setw(20) << item_names[2] << setw(20) << unit_price[2] << setw(10) << quantity[2] << setw(15) << unit_price[2] * quantity[2] << endl; cout << setw(45) << "Total Cost Before Tax: $" << total_cost_before_tax << endl; cout << setw(45) << "Tax: $" << tax << endl; cout << setw(45) << "Total Cost After Tax: $" << total_cost_after_tax << endl; do { cout << "Enter amount of payment from the customer ($): "; cin >> payment_amount; if (payment_amount < total_cost_after_tax) { cout << "Please enter an amount not less than the total cost." << endl; } else { break; // valid input } } while (true); change = payment_amount - total_cost_after_tax; cout << "Amount of Change to be Rendered: $" << change << endl; cout << "Denominations for Change" << endl; for (int i = 0; i < num_denominations; i++) { int num_denomination = change / denominations[i]; if (num_denomination > 0) { cout << setw(15) << denominations_names[i] << ": " << num_denomination << endl; change -= num_denomination * denominations[i]; } } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}