Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调试模式自动关闭?_C++_Debugging_If Statement_Cout_Cin - Fatal编程技术网

C++ 调试模式自动关闭?

C++ 调试模式自动关闭?,c++,debugging,if-statement,cout,cin,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 choo

所以,我开始调试,完成了这么多代码

#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;
}
}

到底发生了什么,我该如何修复它呢?

您的错误是在switch语句中使用了
int
字符

将案例“1”更改为案例1


对于你的信息,C++中的字符根据它们保存为数字,这就是为什么您的程序没有抱怨您试图用输入检查值为

49
'1'

问题是您正在将
shape
读取为
int
,而将
切换为
char
。 您的开关盒应该看起来像
案例1:
,而不是
案例“1”:


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

需要更多信息。它是在你输入一个数字之前突然关闭,还是在你输入一个数字之后突然关闭?很抱歉,当我输入一个数字并按enter键时它就会关闭这就是你告诉它要做的:),请参见下面的答案。好的,我想我需要澄清的是,我只是把它开头的片段和它似乎卡住的地方放在了一起。后面有一个switch语句it@HeatherT我认为这是一个“我们需要看到更多代码”的例子。“似乎被卡住的地方”可能不是它真正被卡住的地方。我一评论完就加了。现在到了啊。让我改变一下,我试着按照课本上的switch语句来做,但我没有意识到它使用的是char类型。非常感谢。我想这只是开关的语法。:)
'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).