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).