验证输入函数c ++简单

时间:2016-10-20 23:04:55

标签: c++

对某些人来说这似乎很简单,但对我来说,我似乎无法弄清楚为什么这不起作用。我知道我可以在while循环中每次复制和粘贴以获得我想要的结果,但是我被告知如果你不得不重复一次写一个函数!我的代码将双重打印数字,即使有人输入8,它仍将进入while循环。希望有人能解释为什么会发生这种情况。

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

感谢您查看我的帖子!

2 个答案:

答案 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()方法是好的。这表明你有很好的直觉。以下是您可以进一步做的事情:

  1. 查找仅由变量相同或不同的代码行。
  2. 考虑通过以下方式缩小它们:
    • 你能使用for-loop吗?
    • 你能把代码放在一个方法中(可能抽象出一个变量?)
    • 您能使用更广泛的条件声明吗? (这就是你在这里发生的事情:如果一个数字是1,2,3,4,5,6或7,它也是> 0和<7。这就是为什么我们有范围。)
  3. 当程序输入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
    }