1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
using namespace std;
constexpr int MINCHOICE {1}, MAXCHOICE {10}; //Minimum and maximum choices
const string names[] {"Bread", "Milk", "Soap", "Eggs", "Deodorant" "Juice", "Chips", "Forks", "Spoons", "Cups"};
void header() {
cout <<"\n What would you like to buy?\n";
for (size_t itm {}; const auto& nam : names)
cout << ++itm << ". " << nam << '\n';
}
int getChoice()
{
int input {};
while (std::cout << " Please enter your choice: " && !(cin >> input) || input < MINCHOICE || input > MAXCHOICE) //Getting user input
if (!std::cin) {
std::cerr << "\n Invalid input!\n\n"; //Error output
std::cin.clear(); //Clear input
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //Error loop
} else
if (input < MINCHOICE || input > MAXCHOICE)
cerr << "\n Sorry, " << input << " wasn't a valid choice\n\n"; //Error output
return input;
}
string getProd(int prodno) {
return names[prodno - 1];
}
double getPrice(const string& product) {
double price {};
cout << "Please enter price for " << product << ": $"; // Asking the user for price
cin >> price;
return price;
}
int getAge() {
int age {};
cout << "Please enter your age: "; // Asking the user for age
cin >> age;
return age;
}
void invoice(const string& product, double price, int age) {
const double SALESTAX {0.08}; //hold tax
const double DISC {0.05}; // hold discount
double totalTAX {}, discount {};
if (product == "Soap" || product == "Deodorant" || product == "Forks" || product == "Spoons" || product == "Cups") // If product is not grocery
totalTAX = SALESTAX * price; // Calculating tax
if (age >= 60) // If age is greater than 60
discount = DISC * price; // Calculating discount
cout << std::fixed << std::setprecision(2); //Fixed, setprecision
cout << "Invoice \n";
cout << "-----------\n";
cout << product << " price: $" << price << '\n'; //Product price
cout << "Tax: $" << totalTAX << '\n'; //Tax output
if (discount > 0) // If discount greater than 0 //Discount check
cout << "Discount: $-" << discount << '\n'; //Displaying discount
cout << "Total: $" << (price + totalTAX - discount) << '\n'; // Displaying total price
cout << "-----------" << '\n';
}
int main()
{
header();
const auto product {getProd(getChoice())};
const auto price {getPrice(product)};
const auto age {getAge()};
invoice(product, price, age);
}
|