【问题标题】:First-chance exception in VC++VC++ 中的第一次机会异常
【发布时间】:2014-09-22 10:53:59
【问题描述】:

以下是我在 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.

为什么执行没有终止?

【问题讨论】:

  • 您按下了Control-C 并询问为什么您的程序出现Control-C 异常?
  • 您是否为Ctrl-C 设置了控制处理程序?
  • 其实我的疑惑是为什么它没有终止?
  • 另外,关于你可能应该问的问题,如果std::cin &gt;&gt; option; 失败,failbit 被设置在流上,所有后续的读取尝试都将失败;因此opt 永远不会改变。你需要check for error, ignore() the invalid input, and clear() the error state
  • @T.C. :当我为option 提供整数值时,它可以正常工作。在我的逻辑中,类型转换应该将 char 转换为整数,因此它应该可以正常工作。不是吗?

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 2013-12-10
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多