Hello seghrein2300,
I agree with
seeplus it is possible, but after 10 I would say the it becomes ridiculous and shortly after that it is ludicrous to continue. I might consider 10 at the most just to show that it can be done.
To start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
constexpr int MAXITEMS{ 3 }; // <--- Added. Based on the number of variables in lines 6 to whatever the end would be.
string userInput; // <--- Changed. Removed "exitCheck".
string
productname1,
productname2,
productname3;
double
quantity1{},
quantity2{},
quantity3{};
double
price1{},
price2{},
price3{};
int qtyItemsTotal{}, count{}; // <--- Changed.
double quantity{}, price{}; // <--- Changed.
double priceTotal{}; // <--- Changed.
|
Because of the changes I made I included all the variables, but the modt important are lines 5 - 16.
Lets say that you want to up this to handle 100 items. You would have to add 97 lines to each variable (productname?, quantity? and price?). That is a total of 291 lines not counting the rest of the code that needs to be added.
Somewhere between 5 and 10 I would have to consider using a vector or array just to keep the code down.
Sometimes when working on a program there comes a time when you just have to put it down and not think about it for awhile. Having done that I had a revelation when I looked at the do while loop. When I looked at the first lines of the loop it just screamed improvement, So I came up with this:
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
|
do
{
cout << "\nProduct name (x) to exit: ";
getline(cin, userInput);
if ((userInput[0] == 'x' || userInput[0] == 'X') || count == MAXITEMS)
{
cout << "\n - OFFICIAL RECEIPT -\n";
cout << "Qty Product name Price\n";
for (int idx = 0; idx < count; idx++)
{
if (idx == 0)
{
cout
<< noshowpoint << setprecision(0) << setw(2) << quantity1 << " "
<< left << setw(13) << productname1
<< right << showpoint << setprecision(2) << setw(7) << price1 * quantity1 << '\n';
}
if (idx == 1)
{
cout
<< noshowpoint << setprecision(0) << setw(2) << quantity2 << " "
<< left << setw(13) << productname2
<< right << showpoint << setprecision(2) << setw(7) << price2 * quantity2 << '\n';
}
if (idx == 2)
{
cout
<< noshowpoint << setprecision(0) << setw(2) <<quantity3 << " "
<< left << setw(13) << productname3
<< right << showpoint << setprecision(2) << setw(7) << price3 * quantity3 << '\n';
}
}
cout << "Total\n";
cout << setw(2) << qtyItemsTotal << " item(s) " << setw(7) << priceTotal;
break;
}
while (cout << "Quantity: " && !(cin >> quantity) || quantity < 1)
{
if (!cin)
{
cerr << "\n Invalid Entry! Must be a number.\n\n";
cin.clear();
cin.ignore(10000, '\n');
}
else if (quantity < 1)
{
cerr << "\n Quantity must be at least 1.\n\n";
}
}
while(cout << "Price : " && !(cin >> price) || price == 0.0)
{
if (!cin)
{
cerr << "\n Invalid Entry! Must be a number.\n\n";
cin.clear();
cin.ignore(10000, '\n');
}
else if (price == 0.0)
{
cerr << "\n Price must be greater than 0.\n\n";
}
}
cin.ignore(); // <--- Moved. Removes the "\n" from the formatted input before the next "getline".
if (count == 0)
{
productname1 = userInput;
quantity1 = quantity;
price1 = price;
}
if (count == 1)
{
productname2 = userInput;
quantity2 = quantity;
price2 = price;
}
if (count == 2)
{
productname3 = userInput;
quantity3 = quantity;
price3 = price;
}
qtyItemsTotal += static_cast<int>(quantity);
priceTotal += quantity * price;
count++;
} while (1);
|
In line 4 you input to "userInput" which you use and later set "productnamr?" to "userInput".
In line 6 your original idea is to check a string against a string. A good idea, but what if "Xxabc" was entered. Now you are checking a string of 5 characters against a string with 1 character. They do not match. The down side to this is that it shows that a "std::string" is just an array. So is using a "std::string" acceptable or should it be considered an array that is not allowed?
I added the last or part, untested though to force the receipt and keep you from trying to enter something that you can not.
The big changes are from line 44 to 101.
The 2 while loops are a good idea since you are using formatted input to enter a number, but if this is more than you can use just take the "cout" and "cin" in the while condition and use just those 2 lines.
The other part of these 2 while loops is that quantity and price are put into a more generic variable to be dealt with in the if statements.
I think the if statements starting at line 76 speak for themselves. The added advantage is the the if statements could be turned into a switch very easy. The switch would be easier to write, maintain and change as needed.
Lines 103 and 105 I changed to better deal with adding to the totals.
This is different than
seeplus's code, but the concepts may still be used with his code.
Andy