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;
}
デバッガでコードをステップ実行したとき、あなたは何を観察しましたか? –
私はC++開発者ではありませんが、例えば 'case '1':' case 1: 'ではありませんか?アポストロフィー ''は、私が信じる文字のまわりに置かなければなりません。 –
プログラムは問題なく開きます。オプション1〜3を選択すると、メニューが再び表示されます。オプション4は必要に応じて動作し、プログラムを閉じます。 – Myles