C ++开关语句异常处理

时间:2019-09-07 21:25:11

标签: c++ exception switch-statement

我正在尝试在我的switch语句中为memnu编写异常处理代码,以防用户输入非int的内容。尝试了许多不同的方法,但是当用户输入字符时仍然会出现连续循环。

我尝试使用std异常,但即使包含include,我的编译器在构建过程中仍会看到错误。

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

class Exam

{

public:

    int loadExam()
    {

        //ifstream infile;
        //string examName = exam;
        ifstream infile("exam.txt");
        streambuf *cinbuf = cin.rdbuf();       //save old buf
        cin.rdbuf(infile.rdbuf());             //redirect std::cin to infile.txt!

        string line, theQuestion, questiontype, theAnswer;
        int  questionvalue;

        //get the number of questions from the first line in the file
        getline(cin,line);
        numquestions = atoi(line.c_str());
        for(int count = 0; count < numquestions; count++){

            getline(cin,line);

            //get the next line with the question type and the value of the question
            int npos = line.size();
            int prev_pos = 0;
            int pos = 0;

            while(line[pos]!=' ')

                pos++;

            questiontype = line.substr(prev_pos, pos-prev_pos);
            prev_pos = ++pos;
            questionvalue = atoi(line.substr(prev_pos, npos-prev_pos).c_str()); // Last word

            //process a true/false question
            if (questiontype == "TF")
            {

                myQuestions[count] = new QuestionTF;
                getline(cin,theQuestion);
                myQuestions[count]->setQuestion(theQuestion,questionvalue);

            }

            //process a multiple choice question
            if (questiontype == "MC")
            {

                myQuestions[count] = new QuestionMC;
                getline(cin,theQuestion);
                myQuestions[count]->setQuestion(theQuestion,questionvalue);

            }
        }

        cin.rdbuf(cinbuf);   //restore cin to standard input
        return numquestions;

    }

    void displayExamQuestions(int numquestions)
    {

        string qtype;

        //print out the questions that have been processed
        for(int count = 0; count<numquestions;count++)

        {

            qtype = myQuestions[count]->getQuestionType();
            cout << qtype << " " << myQuestions[count]->getValue() << "\n";
            myQuestions[count]->printOptions();
            cout << "\n";

        }
    }

private:

    Question *myQuestions[10];
    int numquestions;

};

int main() {

    Exam myExam;
    int numquestions;
    int choice;

    while((choice = displayMenu())!=3)

        switch(choice)
        {
            case 1:
                numquestions = myExam.loadExam();
                break;

            case 2:
                myExam.displayExamQuestions(numquestions);
                break;

            default:
                cout << "Invalid choice.  Try again.\n\n";

        }

   getchar();
    return 0;

}

int displayMenu()
{

    int choice;

    cout << "\t===================== Exam Menu =====================" << endl;
    cout << "\t1.  Load Exam "<<endl;
    cout << "\t2.  Display Exam "<<endl;
    cout << "\t3.  Quit"<<endl;
    cout << "\t=====================================================" << "\n" << endl;
    cout << "Please enter your selection: ";
    cin >> choice;
    cout << "\n" << endl;

    return choice;

}

当用户输入字符或字母字符字符串时,要求输出以读取“无效选择,请重试”。

1 个答案:

答案 0 :(得分:0)

在这种情况下,验证应由displayMenu函数处理,原因有两个。

  1. displayMenu函数表示它将返回一个整数,因此它应负责确保用户输入数字,而不是字符或字符串。

  2. displayMenu列出了选项,因此它知道有多少个可用选项,这意味着它还应该检查整数是否在1到3之间。

Infinite loop with cin when typing string while a number is expected

    int displayMenu() //This function should be responsible for validating that an 
                      // int was inputed
    {
        int choice;

        while (true)
        {
            cout << "\t===================== Exam Menu =====================" << endl;
            cout << "\t1.  Load Exam " << endl;
            cout << "\t2.  Display Exam " << endl;
            cout << "\t3.  Quit" << endl;
            cout << "\t=====================================================" << "\n" << endl;
            cout << "Please enter your selection: ";
            cin >> choice;
            cout << "\n" << endl;

            if (cin.fail())
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n'); //This clears out the stream if they entered a string
                //Try using cin.ignore() and inputing a string to see what happens.
            }
            else if (choice >= 1 && choice <= 3)
            {
                break;
            }
        }

        return choice;
    }

您可以通过使用仅打印菜单的displayMenu函数和不关心输入什么整数的名为getInput的第二个函数来分离第二部分。然后由调用函数确定该值在1到3之间。

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

void displayMenu();
int getInput();

int main() {

    int numquestions;
    int choice = 0;

    while (choice != 3)
    {
        displayMenu();
        while ((choice = getInput()) < 1 || choice > 3) 
        {
            std::cout << "Please pick a value between 1 and 3\n";

            displayMenu();
        }

        switch (choice)
        {
        case 1:
            cout << "Case 1\n";
            break;

        case 2:
            cout << "Case 2\n";
            break;

        default:
            cout << "Invalid choice.  Try again.\n\n";
        }
    }

    getchar();
    return 0;

}

//Only responsible for getting an int
int getInput()
{
    int choice;

    while (true)
    {
        cin >> choice;
        cout << "\n" << endl;

        if (cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

            std::cout << "Please enter a valid number\n";
        }
        else
        {
            break;
        }
    }

    return choice;
}

//This function only displays a menu
void displayMenu()
{
    cout << "\t===================== Exam Menu =====================" << endl;
    cout << "\t1.  Load Exam " << endl;
    cout << "\t2.  Display Exam " << endl;
    cout << "\t3.  Quit" << endl;
    cout << "\t=====================================================" << "\n" << endl;
    cout << "Please enter your selection: ";
}
相关问题