2016-12-07 13 views
1

私は単純なメニュー駆動型の変換プログラムを作成していましたが、何とか関数の1つに無限ループを作成してしまいました。 。基本的なメニュー駆動型プログラムC++、無限ループ

2番目の機能はうまく機能しているようです。 アドバイスやヒントは非常に高く評価されています。

#include <iostream> 
#include <cmath> 

using namespace std; 

void showChoices(); 
double miles(double, double); 
double degf(double, double); 

int main() 
{ 
    double x, y; 
    int choice; 
    do 
    { 
     showChoices(); 
     cin >> choice; 
     switch (choice) 
     { 
      case 1: 
       cout << "Input miles to be converted, enter * to submit: \n"; 
       cin >> x >> y; 
       cout << x << " is " << miles(x,y) << " in kilometers" << endl; 
       break; 
      case 2: 
       cout << "Input degrees (in Farenheit) to be converted, enter * to submit: \n"; 
       cin >> x >> y; 
       cout << x << " is " << degf(x,y) << " in degrees Celsius" << endl; 
       break; 
     } 
    } 
    while (choice != 2); 
    return 0; 
} 
void showChoices() 
{ 
    cout << "MENU" << endl; 
    cout << "1: Miles to Kilometers " << endl; 
    cout << "2: Farenheit to Celsius " << endl; 
} 
double miles(double mi, double km) 
{ 
    return km = mi * 1.609344; 
} 
double degf(double fah, double cel) 
{ 
    return cel = 5*(fah-32)/9; 
} 
+0

すでにバグが発見されているので、私はあなたに "デフォルト"のケースを提案し、あなたがループを制御するより良い方法であるかどうかを確認するフィールドを追加することができます。 –

答えて

3

選択肢は1:whileループは常にtrueでループ内に入ります。

3として選択肢を追加:終了する。 while条件を(choice!= 3)に変更すると、ループが中断されます。

関連する問題