2016-07-08 11 views
0

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; 
} 
+0

デバッガでコードをステップ実行したとき、あなたは何を観察しましたか? –

+1

私はC++開発者ではありませんが、例えば 'case '1':' case 1: 'ではありませんか?アポストロフィー ''は、私が信じる文字のまわりに置かなければなりません。 –

+0

プログラムは問題なく開きます。オプション1〜3を選択すると、メニューが再び表示されます。オプション4は必要に応じて動作し、プログラムを閉じます。 – Myles

答えて

3

DisplayMenu()は、intを返します。しかし、あなたのcaseステートメントはcharリテラルを使用しています。 charintを比較すると、それは文字のコードを使用します。 case '1':は同等ですcase 49:です。整数リテラルを使用するようにケースを変更します。

case 1: 

などとなる。

+0

ありがとうございます。これは私の問題を解決しました。プログラム全体が今のように動作します。私は本当に助けに感謝します。 – Myles

+0

@Myles:問題を解決した場合は、これを受け入れられた回答としてください。これは、答えの左側にあるチェックマークを押して行うことができます。そうすることで、問題が解決されたものとマークされ、回答者(この場合はBarmar)には15の評判ポイント、2つの評判ポイントを持つ質問者(あなた)に報酬を与えます。 –

+0

@Myles:回答を受け入れたものとしてマークすると、この解決策があなたと同じ問題を抱えている他のユーザーにも表示されます。 –

関連する問題