为什么会出现无限循环而不是异常?

时间:2020-04-26 14:48:27

标签: c++ xcode

我制作了一个程序,该程序将具有一些与我的结构配合使用的功能。我尚未添加功能,但是用户界面存在问题。当我输入“ hello”而不是int(用于choice&choice2)时,我期望得到异常,但是却得到无限循环。如果“ hello”是一个字符数组,并且字符被转换为相应的ASCII码,我不明白为什么循环是无限的。我需要例外才能抓到它。

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define MARK_AMOUNT 4

struct student{
    string name;
    string group;
    int mark[MARK_AMOUNT];
};

istream& operator >> (istream& in, student& student) {
    in >> student.name >> student.group;
    for (int i = 0; i < MARK_AMOUNT; i++) in >> student.mark[i];
    return in;
}

ostream& operator << (ostream& out, const student& student) {
    out << student.name << "\t" << student.group << "\t";
    for (int i = 0; i < MARK_AMOUNT; i++) out << student.mark[i] << " ";
    return out;
}

int main(){
    vector <student> list;
    int choice, choice2;
    ifstream myfile;
    bool input = false, input2 = false;
    while(!input){
        cout << "Input:\n1 to work with files\n2 to work in console\n";
        cin >> choice;
        if(choice == 1 || choice == 2) input = true;
        else cout << "Wrong number.\n";
    }
    while(!input2){
        cout << "Input function number[1-3]: ";
        cin >> choice2;
        if(choice2 >= 1 && choice2 <= 3) input = true;
        else cout << "Wrong number.\n";
    }
    switch (choice) {
        case 1:
        {
            string fileName;
            cout << "Input name of file: ";
            cin >> fileName;
            ifstream myfile(fileName);
            student temp;
            while(myfile >> temp){
                list.push_back(temp);
            }
        }
            break;
        case 2:
            break;
        default:
            break;
    }
    switch (choice2) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
    switch (choice) {
        case 1:
        {
            myfile.close();
            break;
        }
        case 2:
            break;
        default:
            break;
    }
    return 0;
}

输出:

Input:
1 to work with files
2 to work in console
hello
Wrong number.
Input:
1 to work with files
2 to work in console
Wrong number.
Input:
1 to work with files
2 to work in console
Wrong number.
...

...

1 个答案:

答案 0 :(得分:0)

如果要继续执行,则绝对不需要例外。 您需要一种机制来首先检查输入是否有效,因为std::cin进入错误状态并将输入保留在其缓冲区中,从而跳过下一个调用。

以下是您可以依靠的示例:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(){

    int choice, choice2;   
    bool input = false, input2 = false;

    std::string line;

    while(!input){

        cout << "Input:\n1 to work with files\n2 to work in console" << endl;
        while (std::getline(std::cin, line))
            {

                std::stringstream ss(line);
                if (ss >> choice)
                {
                    if (ss.eof())
                    {   // Success
                        break;
                    }
                }
                std::cout << "Invalid Input, try again" << endl;
            }

        if(choice == 1 || choice == 2) input = true;
        else {
            cout << "Wrong number.\n" << endl;

        }
    }
}