VC ++中的第一次机会异常

时间:2014-09-22 10:54:00

标签: c++ visual-c++ first-chance-exception

以下是我在VS2010中编码的VC ++代码的一部分。

do
{

    std:: cout << "\nOPTIONS:\n";
    std:: cout << "\n1. Get all session data.\n";
    std:: cout << "\n2. Get Nth session data\n";
    std:: cout << "\n3. Get Next session data\n";
    std:: cout << "\n4. Get Previous session data\n";
    std:: cout << "\n5. Get total no of sessions\n";
    std:: cout << "\n6. Exit\n";
    std:: cout << "\nEnter Your Option: ";

    std :: cin >> option;

    switch(int(option))
    {
         case 1:
            {
                data.ReadSetupFile();
                 break;
            }

        case 2:
            {
                break;
            }

        .....
        ......

        case 6:
            {

                std::cout<<"\nARE YOU SURE YOU WANT TO EXIT?(Press y for yes and any other character for no)\n";
                cin >> opt;
                break;
            }

        default:
            {
                std::cout << "\nINAVALID OPTION!!TRY AGAIN\n";
                break;
             }

    }

    std::cout<<"\nDO YOU WANT TO CONTINUE?(Press y for yes and any other character for no)\n";
    cin >> opt;

  }while(opt=='y');

但是,如果我插入一个字符而不是一个整数作为option的值,则菜单选项将在输出控制台中打印而不会终止,直到我使用ctrl+c。在使用此中断策略时,我收到消息:

First-chance exception at 0x75936da7 in SetupAPIReader.exe: 0x40010005: Control-C.

为什么执行没有终止?

1 个答案:

答案 0 :(得分:0)

如果给定输入不是预期类型,则设置failbit cin。检查此位并清除将把值重置为变量的初始值。

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