gpt4 book ai didi

c++ - 开关盒未打开,通过返回菜单功能获得选择

转载 作者:行者123 更新时间:2023-11-28 00:01:52 25 4
gpt4 key购买 nike

我在 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;
}

最佳答案

DisplayMenu() 返回 int。但是您的 case 语句使用的是 char 文字。当比较 charint 时,它使用字符的代码,例如case '1': 等同于 case 49:。更改您的案例以使用整数文字。

case 1:

等等。

关于c++ - 开关盒未打开,通过返回菜单功能获得选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com