int main()
{
int option = selectionHelper();
cout << selectionHelper() << endl;
cout << endl;
if(option == 8)
{
cout << "Exiting program..." << endl;
cout << endl;
cin >> option;
}
while (option != 8)
{
if (option == 1){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 2){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 3){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 4){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 5){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 6){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else if(option == 7){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else{
cout << "Invalid input... Please try again..." << endl;
cout << endl;
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}//end else if statement
}//end while loop
}//end function main
现在我的功能:
int selectionHelper()
{
int option;
cout << "1. Initialize seating for new performance." << endl;
cout << "2. View seating chart." << endl;
cout << "3. Reserve seats." << endl;
cout << "4. Calculate tickets remaining in row." << endl;
cout << "5. Calculate tickets remaining in theater." << endl;
cout << "6. Calculate total tickets sold." << endl;
cout << "7. Calculate ticket sales." << endl;
cout << "8. Exit program." << endl;
cout << "Option: " << endl;
cin >> option;
return option;
}//end selectionHelper
感谢您查看我的帖子!
答案 0 :(得分:0)
您必须通过以下方式修改代码:
int option = selectionHelper();
cout << option << endl;
还有:
if(option == 8)
{
cout << "Exiting program..." << endl;
}
您可以简化代码(部分使用cuniculus进行的简化,同时使用option
将错误修复为您自己的错误代码):
#include <iostream>
using namespace std;
int selectionHelper()
{
int option;
cout << "1. Initialize seating for new performance." << endl;
cout << "2. View seating chart." << endl;
cout << "3. Reserve seats." << endl;
cout << "4. Calculate tickets remaining in row." << endl;
cout << "5. Calculate tickets remaining in theater." << endl;
cout << "6. Calculate total tickets sold." << endl;
cout << "7. Calculate ticket sales." << endl;
cout << "8. Exit program." << endl;
cout << "Option: " << endl;
cin >> option;
return option;
}//end selectionHelper
int main()
{
int option = selectionHelper();
while (option != 8)
{
if (option > 0 && option < 8){
option = selectionHelper();
}else{
cout << "Invalid input... Please try again..." << ends << endl;
option = selectionHelper();
}//end else if statement
}
cout << "Exiting program..." << endl;
return 0;
}//end function main
如果只需要一次设置selectionHelper
变量,那么您的错误基本上就是调用option
次数太多了。你在整个if-else结构中也有类似的事情发生了 - 但是如果需要的话,我会把它留给你解决,解决方案是一样的。
答案 1 :(得分:0)
这个答案和其他人会告诉你要用什么代码来解决你的问题。如果您将来遇到类似的问题,请尝试使用调试器遍历您的代码。 (经验也有帮助。)关于代码冗余:首先,你使用selectionHelper()方法是好的。这表明你有很好的直觉。以下是您可以进一步做的事情:
当程序输入8时程序继续运行,因为你要求他们在输入8之后输入一个数字!所以你的第一块代码可以更简单:
int option = selectionHelper();
cout << option << endl <<endl; // <-- output option, don't call selectionHelper() here again, that's why you get double printing
if(option == 8)
{
cout << "Exiting program..." << endl;
cout << endl;
//cin >> option; // <-- don't input here again!
}
简化代码的关键是要认识到在少数情况下只能编写不同的代码。所以,这是你的while
循环:
while (option != 8)
{
if (option > 0 && option < 8){
cout << selectionHelper() << endl;
cout << endl;
cin >> option;
}else{
cout << "Invalid input... Please try again..." << ends << endl;
cout << selectionHelper() << endl << endl;
cin >> option;
}//end else if statement
}