开关盒没有打开,通过返回菜单功能获得选择

时间:2016-07-08 23:38:06

标签: c++

我在do-while循环中使用开关时遇到问题。菜单正在显示,但选择后它只是再次显示菜单而不是打开正确的开关盒。非常感谢和帮助。我一直在寻求帮助,似乎找不到多少。

#include <iostream>
using namespace std;

//function prototypes
int DisplayMenu(); //shows menu and returns input
double CalcAreaCircle(double radius ); //returns the area of the circle
double CalcAreaRectangle(double length, double width ); //returns the area     of a rectangle
double CalcAreaTriangle(double base, double height ); //returns the area of a triangle

int Choice;
double AreaOfCircle;
double radius;
double AreaOfRectangle;
double length;
double width;
double AreaOfTriangle;
double base;
double height;

//function main
int main()
{

    Choice = -1;
    while (Choice != 4)
    {
        Choice = DisplayMenu();
        switch (Choice)
            {
                case '1':
                    {
                    cout << "What is the radius of the circle?" << endl;
                    cin >> radius;
                    cout << endl;
                    AreaOfCircle = CalcAreaCircle(radius);
                    cout << endl << "The area of your circle is " << AreaOfCircle << endl;
                    break;
                    }
                case '2':
                    {
                    cout << "what is the length of the rectangle?" << endl;
                    cin >> length;
                    cout << endl << "What is the width of the rectangle?" << endl;
                    cin >> width;
                    cout << endl;
                    AreaOfRectangle = CalcAreaRectangle(length, width);
                    cout << endl << "The area of your rectangle is " << AreaOfRectangle << endl;
                    break;
                    }
                case '3':
                    {
                    cout << "What is the base of the triangle?" << endl;
                    cin >> base;
                    cout << endl << "What is the height of the triangle?" << endl;
                    cin >> height;
                    cout << endl;
                    AreaOfTriangle = CalcAreaTriangle(base, height);
                    cout << endl << "The area of your triangle is " << AreaOfTriangle << endl;
                    break;
                    }
            }
    }

system ("pause");
return 0;
}

//function DisplayMenu
int DisplayMenu()
{
int selection;

cout << "What would you like to know the area of?" << endl;
cout << "\t1. Area of a Circle." << endl;
cout << "\t2. Area of a Rectangle." << endl;
cout << "\t3. Area of a Triangle." << endl;
cout << "\t4. Quit." << endl;
cin >> selection;

while (selection < 1 || selection > 4)
{
    cout << "Please enter a valid option." << endl;
    cin >> selection;
    cout << endl;
}

return selection;
}

//function CalcAreaCircle
double CalcAreaCircle(double radius)
{
double area;
const double PI = 3.14159;

area = PI * (area * area);

return area;
}

//function CalcAreaRectangle
double CalcAreaRectangle(double length, double width)
{
double area;
area = length * width;

return area;
}

//function CalcAreaTriangle
double CalcAreaTriangle(double base, double height)
{
double area;
area = base * height;

return area;
}

1 个答案:

答案 0 :(得分:3)

DisplayMenu()返回int。但是,您的case语句正在使用char文字。将charint进行比较时,它会使用字符代码,例如case '1':相当于case 49:。将您的案例更改为使用整数文字。

case 1:

等等。