基本菜单驱动程序C ++,无限循环

时间:2016-12-07 09:22:29

标签: c++ infinite-loop

我一直在创建一个简单的菜单驱动转换程序,但不知何故在其中一个函数中创建了一个无限循环(英里到一公里)并且不知道如何修复它。

第二个功能似乎工作正常。 任何建议或提示都非常感谢。

#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;
}

1 个答案:

答案 0 :(得分:3)

选择是1:while循环始终为true,因此它循环。

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