I've been writing an assignment where we're tasked with making a shopping cart program that tracks the customer's cumulative price.
I'm not allowed to use strings, global variables, or user defined functions I'm strictly instructed to use only Character arrays and loops.
While my shopping cart program works fine, I was wondering if there is any way to simplify my code? I feel like I would be penalized for making the code overly complicated. I feel like my quit feature in particular is overly complicated. Is there a better, simpler way of implementing it without using strings or functions?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
char continueOrQuit;
char productName[900];
float productPrice;
float totalCost = 0;
int productQuantity = 0;
bool validResponse;
cout << "Welcome to SmartCart" << endl;
cout << "Simply enter the name of your product when prompted" << endl;
cout << "After you enter your product, enter the price when prompted \n\n";
//while ((productName[900] != 'D', 'o', 'n', 'e') || (productName[900] != 'd', 'o', 'n', 'e'))
//While im not done shopping
do
{
cout << "Name of Product: "; // Prompt for product
cin.getline(productName, 900); // Get the name
cout << endl;
cout << "Cost of Product: "; // Prompt for product cost
cin >> productPrice; // Get the cost
while ((!cin) || (productPrice < 0))
{
cout << "Invalid Input!! Try again!!" << endl << endl;
cout << "Cost of Product: "; // Prompt again for product cost
cin.clear();
cin.ignore(100, '\n');
cin >> productPrice;
}
cin.ignore(250, '\n'); // Ignore the rest of the garbage
cout << endl;
// if everything is correct, we set up the display and give the results.
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The item(s) \"" << productName << "\" has/have been added to your cart for $"
<< productPrice << endl;
totalCost = totalCost + productPrice; // Calculating the cumulative sum total
cout << "Your shopping total so far is: $" << totalCost << endl; // Display the sum total
productQuantity++; // Count the number of items in cart
cout << "You have " << productQuantity << " item(s) in your cart." << endl;
// Display the amount of characters in the cart
cout << "To quit shopping, type \"Q\". Otherwise, type \"C\" (Without quotation marks)"
<< endl;
cout << "Would you like to continue shopping? (C/Q) : ";
cin >> continueOrQuit;
cin.ignore(100, '\n');
continueOrQuit = tolower(continueOrQuit);
if (continueOrQuit == 'q')
{
cout << "You have chosen to finish and check out." << endl;
validResponse = true;
}
else if (continueOrQuit == 'c')
validResponse = true;
else
cout << "You have to type either C or Q!" << endl;
validResponse = false;
while (!validResponse)
{
cout << "Would you like to continue shopping? (C/Q) : ";
cin >> continueOrQuit;
cin.ignore(100, '\n');
continueOrQuit = tolower(continueOrQuit);
if (continueOrQuit == 'q')
{
cout << "You have chosen to finish and check out." << endl;
validResponse = true;
}
else if (continueOrQuit == 'c')
validResponse = true;
}
} while (continueOrQuit == 'c');
cout << "Your checkout total is $" << totalCost << endl;
cout << "You are purchasing a total of " << productQuantity << endl;
system("PAUSE");
return 0;
}`
Here a shorter version of your code.
mainly I removed extra code when you check input by putting a do while
so the case of wrong input the re-input go back to the first try.
You also have a missing bracket in the last else, it cause you to re-enter q or c in case you already enter it correctly.
Here is the code, you can compare to the original.
int main()
{
char continueOrQuit;
char productName[900];
float productPrice;
float totalCost = 0;
int productQuantity = 0;
bool validResponse;
cout << "Welcome to SmartCart" << endl;
cout << "Simply enter the name of your product when prompted" << endl;
cout << "After you enter your product, enter the price when prompted \n\n";
do
{
cout << "Name of Product: "; // Prompt for product
cin.getline(productName, 900); // Get the name
cout << endl;
do{
cout << "Cost of Product: "; // Prompt for product cost
cin >> productPrice; // Get the cost
if(!cin || productPrice < 0){
cout << "Invalid Input!! Try again!!" << endl << endl;
cin.clear();
cin.ignore(100, '\n');
}
}while ((!cin) || (productPrice < 0));
cin.ignore(250, '\n'); // Ignore the rest of the garbage
cout << endl;
// if everything is correct, we set up the display and give the results.
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The item(s) \"" << productName << "\" has/have been added to your cart for $"
<< productPrice << endl;
totalCost = totalCost + productPrice; // Calculating the cumulative sum total
cout << "Your shopping total so far is: $" << totalCost << endl; // Display the sum total
productQuantity++; // Count the number of items in cart
cout << "You have " << productQuantity << " item(s) in your cart." << endl;
// Display the amount of characters in the cart
do{
cout << "To quit shopping, type \"Q\". Otherwise, type \"C\" (Without quotation marks)"
<< endl;
cout << "Would you like to continue shopping? (C/Q) : ";
cin >> continueOrQuit;
cin.ignore(100, '\n');
continueOrQuit = tolower(continueOrQuit);
if(continueOrQuit == 'q' || continueOrQuit == 'c')
validResponse = true;
else
{
cout << "You have to type either C or Q!" << endl;
validResponse = false;
}
}while(!validResponse);
if (continueOrQuit == 'q')
{
cout << "You have chosen to finish and check out." << endl;
}
} while (continueOrQuit == 'c');
cout << "Your checkout total is $" << totalCost << endl;
cout << "You are purchasing a total of " << productQuantity << endl;
system("PAUSE");
return 0;
}