0

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

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

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

4

2 回答 2

1

问题是您正在阅读shapeintswitch将其作为char. 您的 switch 案例应该看起来像case 1:,而不是case '1':.

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

于 2013-02-10T00:59:44.167 回答
1

您的错误是您在switch 语句中接受int并使用了 a 。character

更改case '1':case 1:

供您参考,C++ 中的字符根据它们的ascii 值保存为数字,这就是为什么您的程序不会抱怨您尝试检查输入'1'值的原因49

于 2013-02-10T00:32:16.960 回答