Hello akeilo,
Here is something to think about:
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream>
#include <iomanip>
#include <limits> // <--- Added.
#include <string>
using namespace std;
int main()
{
constexpr int MINCHOICE{ 1 }, MAXCHOICE{ 10 }; // <--- Change "MAXCHOICE" if menu choices change.
//constexpr double prices[]{ 0.0, 1.50, 2.50 }; // <--- Why are you letting the user enter a price?
string product; // this variable is used to store the product bought. Empty when defined. Does not need initialized.
int input; // to store user input
std::cout << std::fixed << std::setprecision(2); // <--- Only needs done once. I usually put this at the top.
cout <<
"\n"
" What would you like to buy?\n"
" 1. Bread:\n"
" 2. Milk:\n"
" 3. Soap:\n"
" 4. Eggs:\n"
" 5. Deodorant:\n"
" 6. Juice:\n"
" 7. Chips:\n"
" 8. Forks:\n"
" 9. Spoons:\n"
" 10. Cups:\n";
// <--- Consider 11 Exit.
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";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
else if (input < MINCHOICE || input > MAXCHOICE)
{
cerr << "\n Sorry, " << input << " wasn't a valid choice\n\n";
}
}
//{ // <--- You do not need the {}s.
if (input == 1)
{
product = "Bread";
}
else if (input == 2)
{
product = "Milk";
}
else if (input == 3)
{
product = "Soap";
}
else if (input == 4)
{
product = "Eggs";
}
else if (input == 5)
{
product = "Deodorant";
}
else if (input == 6)
{
product = "Juice";
}
else if (input == 7)
{
product = "Chips";
}
else if (input == 8)
{
product = "Forks";
}
else if (input == 9)
{
product = "Spoons";
}
else if (input == 10)
{
product = "Cups";
}
if (product != "")
{
double price; // To store price of product
int age; // to store age of customer
cout << "Please enter price for " << product << ": $"; // Asking the user for price
cin >> price;
cout << "Please enter your age: "; // Asking the user for age
cin >> age;
double tax = 0;
if (product == "Soap", "Deodorant") // If product is not grocery
{
tax = 0.08 * price; // Calculating tax
}
double discount = 0;
if (age >= 60) // If age is greater than 60
{
discount = 0.05 * price; // Calculating discount
}
cout << "Invoice " << endl;
cout << "-----------" << endl;
cout << product << " price: $" << fixed << setprecision(2) << price << endl;
cout << "Tax: $" << fixed << setprecision(2) << tax << endl;
if (discount > 0) // If discount greater than 0
{
cout << "Discount: $-" << fixed << setprecision(2) << discount << endl;
}
double total = price + tax - discount; // Calculating total price
cout << "Total: $" << fixed << setprecision(2) << total << endl; // Displaying total price
}
//}
}
|
This is a start.
There are some other things I have noticed. I am think of something like
constexpr double SALESTAX{ 0.08 };
. You are letting the user enter a price. This should be part of the program and just used. That would be line 11.
I was wondering if you have learned about "switch/case" yet?
Your program only runs once allowing only 1 item to be chosen. Eventually this would be better in a loop allowing multiple choices.
Prefer using "double"s over "floats" These days a number like "0.08" is considered a double and trying to stuff that, or a calculation, into a float will have data loss.
Another thing is to prefer using the new line, (\n), over "endl". "endl" is a function that takes time to process. The more you have the longer it takes the program to run.
Line 19 demonstrates that you do not need a "cout" for every line. In this case what looks like 12 individual strings is considered 1 big string by the IDE and compiler. One advantage is that it looks more like what will be displayed on the screen, so you have a better idea of what it will look like.
The biggest change starts at line 33. This is designed to check for a non numeric input and out of the valid range.
See what you think.
Andy