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

C++ 基本菜单驱动程序C++;,无限循环,c++,infinite-loop,C++,Infinite Loop,我一直在创建一个简单的菜单驱动转换程序,但不知怎的在其中一个函数(英里到公里)中创建了一个无限循环,不知道如何修复它 第二个函数似乎工作得很好。 非常感谢您的任何建议或提示 #include <iostream> #include <cmath> using namespace std; void showChoices(); double miles(double, double); double degf(double, double); int main ()

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

第二个函数似乎工作得很好。 非常感谢您的任何建议或提示

#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;
}
#包括
#包括
使用名称空间std;
void showChoices();
双倍英里(双倍,双倍);
双degf(双,双);
int main()
{
双x,y;
智力选择;
做
{
showChoices();
cin>>选择;
开关(选择)
{
案例1:
cout>x>>y;

coutFor choice为1:while循环始终为真,因此它进入循环

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

由于错误已经被发现,我建议您使用“default”案例,并且您可以添加一个字段来检查是否要继续,或者是否需要更好的方法来控制循环。