2016-12-07 61 views
1

我已经创建一个简单的菜单驱动的转换程序,但在功能(英里到公里一个)一个以某种方式创造了一个无限循环,不知道如何解决它。基本菜单驱动程序C++,无限循环

第二个函数似乎工作得很好。 任何意见或建议非常感谢。

#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

正如已经发现的bug,我会建议你有“默认”的情况下,也可以是你可以添加一个字段来检查,如果你想进一步继续,或者没有更好的方法来控制你的循环。 –

回答

3

可供选择的是1 while循环总是为true,因此它循环进行。

添加选项为3:退出。 将while条件更改为(choice!= 3),这将打破循环。