调试模式自发关闭?

时间:2013-02-10 00:27:55

标签: c++ debugging if-statement cout cin

所以,我开始调试,并通过这么多代码,

#include <cmath>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    double radius,width,length,height,area,base;
    int shape;
    const double pi =3.14159;
    cout<< "Please choose from the following menu. \n"
            "Geometry Calculator \n"
            "1. Calculate the Area of a Circle \n"
            "2. Calculate the Area of a Rectangle\n"
            "3. Calculate the Area of a Triangle\n"
            "4. Quit\n";
    cin>>shape;
    if(shape>4 || shape < 1)
    {
        cout<<"Your selection was not acceptable.\n\a\a"
              "Please choose from the following menu. \n"
              "Geometry Calculator \n"
              "1. Calculate the Area of a Circle \n"
              "2. Calculate the Area of a Rectangle\n"
              "3. Calculate the Area of a Triangle\n"
              "4. Quit\n";
    }
switch (shape)
{
case '1':
    cout<<"What is the radius of the circle?\n";
    cin>>radius;
    if(radius<0)
    {
        cout<<"Please enter a non-negative radius.\n\a";
        cin>>radius;
    }

    area = pow(radius,2) * pi;

    cout<<"Your circle has an area of " <<area<<".";
    break;


case '2':
    cout<<"What is the width of the rectangle?\n";
    cin>>width;
    if(width<0)
    {
        cout<<"Please enter a non-negative width.\n\a";
        cin>>width;
    }
    cout<<"What is the length of the rectangle?\n";
    cin>>length;
    if(length<0)
    {
        cout<<"Please enter a non-negative length.\n\a";
        cin>>length;
    }
    area = length * width;
    cout<<"The area of your rectangle is " <<area<<".\n";
    break;

case '3':
    cout<<"What is the base of the triangle?\n";
    cin>>base;
    if(base<0)
    {
        cout<<"Please enter a non-negative base measurement.\n\a";
        cin>>base;
    }
    cout<<"What is the height of the triangle?\n";
    cin>>height;
    if(height<0)
    {
        cout<<"Please enter a non-negative height measurement.\n\a";
        cin>>height;
    }
    area = base*height*.5;
    cout<<"Your triangle's area is "<<area<<".\n";
    break;
}
}

等等 - 我认为它实际上停在了cin。调试窗口突然关闭,然后在出现这种情况时显示在输出窗口中:

'heather t chapter 4 21.exe' (Win32): Loaded 'C:\Users\Heather\Documents\Visual Studio 2012\Projects\heather t chapter 4 21\Debug\heather t chapter 4 21.exe'. Symbols loaded.
'heather t chapter 4 21.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'heather t chapter 4 21.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'heather t chapter 4 21.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'heather t chapter 4 21.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp110d.dll'. Symbols loaded.
'heather t chapter 4 21.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110d.dll'. Symbols loaded.
The program '[3800] heather t chapter 4 21.exe' has exited with code 0 (0x0).

世界上发生了什么,我该如何解决?

2 个答案:

答案 0 :(得分:1)

您的错误是您在切换声明中使用int并使用character

case '1':更改为case 1:

为了您的信息,C ++中的字符将根据其ascii value保存为数字,这就是为什么您的程序没有抱怨您尝试检查'1'的值49与您的意见。

答案 1 :(得分:1)

问题是您正在以shape的身份阅读int,但switch正在char。 您的切换案例应该看起来像case 1:,而不是case '1':

要防止程序在完成后关闭控制台,您可以添加cin.get()以等待代码最后一键按下。