我一直在编写一项任务,我们负责制作跟踪客户累积价格的购物车计划。
我不允许使用字符串,全局变量或用户定义的函数 我严格指示只使用字符数组和循环。
虽然我的购物车程序运行良好,但我想知道是否有任何方法可以简化我的代码?我觉得因为代码过于复杂而受到惩罚。我觉得我的退出功能特别复杂。有没有更好,更简单的方法来实现它而不使用字符串或函数?
#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;
}`
答案 0 :(得分:0)
这是您的代码的缩写版本。
主要是我通过放置do while
检查输入时删除了额外的代码,所以输入错误的情况重新输入回到第一次尝试。
在最后的其他地方你也有一个缺失的括号,它会导致你重新输入q或c,以防你已经正确输入。
这是代码,您可以与原始代码进行比较。
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;
}