无法将菜单选择获取到 运行 功能。 C++

Cannot get menu selection to run a function. C++

我希望创建代码来调用菜单选择中的函数。我希望菜单选择转换公斤和磅的动作。我当前代码中的菜单选择未完成此任务。我怎样才能让菜单选择工作?我想让它把公斤换算成磅,把磅换算成公斤,然后退出程序。菜单选择应循环选择 3 以退出程序。

#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes
void displayMenu();
int getChoice(int min, int max);
double kilosToPounds(double);
double poundsToKilos(double);

/*****     main     *****/
int main()
{
    double weight = 0;
    int choice = 0;

    displayMenu();

    cout << "Please choose a function: ";
    cin >> choice;

    choice = getChoice(1, 3);
    
    while (choice == 1)
    {
        double kilosToPounds(weight);
    }
    while (choice == 2)
    {
        double kilosToPounds(weight);
    }
    while (choice == 3);
    {
        return 0;
    }
}

/*****     displayMenu     *****/
void displayMenu()
{
    int padding = 8;

    cout << "Program to convert weights:\n\n"
        << right
        << setw(padding) << "" << "1. Convert kilograms to pounds\n"
        << setw(padding) << "" << "2. Convert pounds to kilograms\n"
        << setw(padding) << "" << "3. Quit\n";

}


/*****     getChoice     *****/
// THIS IS THE SAME FUNCTION YOU WROTE EARLIER IN THIS SET
// OF LAB EXERCISES. JUST FIND IT AND PASTE IT HERE. 
int getChoice(int min, int max)
{
    int choice;
    // Get and validate the input
    cin >> choice;
    while (choice < min || choice > max)
    {
        cout << "Invalid input. Enter an choice between 1 & 3: ";
    }
    return choice;
}

/*****     kilosToPounds     *****/
double kilosToPounds(double weight)
{
    double kilo = weight / 2.2;
    cout << "This item weighs " << kilo << " kilos.\n";
    return 0;
}

/*****    poundsToKilos     *****/
double poundsToKilos(double weight)
{
    double pounds = weight * 2.2;
    cout << "This item weighs " << pounds << " pounds.\n";
    return 0;
}
  1. 您确定要在此处使用 while 循环吗?
    while (choice == 1)
    {
        double kilosToPounds(weight);
    }
  1. 如果 choice == 1,你将如何以及何时跳出循环?

另外,您可能想用您正在计算的值做一些事情:

        double weightInPounds = kilosToPounds(weight);

您的代码中有几个错误:

  1. 如果您已经有接受用户输入的 getChoice 函数,为什么要在调用 getChoice 函数之前接受用户输入?
    cout << "Please choose a function: ";
    cin >> choice;   // <-- not needed
  1. 如果只想检查特定条件,为什么要使用 while 循环?请改用 if 语句。
if (choice == 1) {...}

而不是

while (choice == 1) {...}
  1. 调用函数时,不需要指定其return类型。
auto pounds = kilosToPounds(weight);

或者只是

kilosToPounds(weight);

而不是

double kilosToPounds(weight);

Demo

见下文,

       cout << "Please choose a function: ";
       cin >> choice;

删除cin>>选择;因为您正在从 getChoice(int,int); 中获得选择功能。所以这里没有必要。

并且在 getChoice(int,int) 函数中,

    int getChoice(int min, int max)
    {
      int choice;
      // Get and validate the input
      cin >> choice;
      while (choice < min || choice > max)
      {
          cout << "Invalid input. Enter an choice between 1 & 3: ";
      }
       return choice;
    }

您在 while 循环中没有输入,因此如果您输入的选项小于最小值或大于最大值,则循环将进入无限循环,因为选项没有变化 variable.So 在循环中进行输入。

在main()中

如果选择等于 1 或 2,它会因此进入无限循环

             while (choice == 1)..

并在 main() 中

             double kilosToPounds(weight);

这不是调用函数的方式,你没有权重计算。

完整代码:

#include <iostream>
#include <iomanip>
using namespace std;


void displayMenu();
int getChoice(int,int);
double kilosToPounds(double);
double poundsToKilos(double);


int main()
{
    double weight = 0;
    int choice = 0;

    displayMenu();

    cout << "Please choose a function: ";
    choice = getChoice(1, 3);

    if (choice == 1)
    {
        cin>>weight;
        kilosToPounds(weight);
    }
    if (choice == 2)
    {
        cin>>weight;
        kilosToPounds(weight);
    }
    if (choice == 3);
    {
        return 0;
    } 
 }


void displayMenu()
{
    int padding = 8;

    cout << "Program to convert weights:\n\n"
         << right
         << setw(padding) << "" << "1. Convert kilograms to pounds"<<endl
         << setw(padding) << "" << "2. Convert pounds to kilograms"<<endl
         << setw(padding) << "" << "3. Quit"<<endl;

}


int getChoice(int min, int max)
{
    int choice;
    cin >> choice;
    while (choice < min || choice > max)
    {
       cout << "Invalid input. Enter an choice between 1 & 3: ";
       cin >> choice;
    }
    return choice;
}

double kilosToPounds(double weight)
{
     double kilo = weight / 2.2;
     cout << "This item weighs " << kilo << " kilos.\n"<<endl;
     return 0;
}


double poundsToKilos(double weight)
{
    double pounds = weight * 2.2;
    cout << "This item weighs " << pounds << " pounds."<<endl;;
    return 0;
}

我重新审视了这个问题,并且能够使用 case 语句和 while 循环创建解决方案:

#include <iostream>
#include <iomanip>

using namespace std;

int choice = 0;
double weight = 0;
double pounds = 0;
double kilo = 0;

int displayMenu()
{
    int padding = 8;

    cout << "Program to convert weights:\n\n"
        << right
        << setw(padding) << "" << "1. Convert kilograms to pounds" << endl
        << setw(padding) << "" << "2. Convert pounds to kilograms" << endl
        << setw(padding) << "" << "3. Quit\n" << endl;
    cout << "Please choose a function: ";
    cin >> choice;
    return choice;
}

int getChoice(int choice)
{
    switch (choice)
    {
    case 1:
        cout << "Enter your weight in kilograms: ";
        cin >> weight;
        kilo = weight / 2.2;
        cout << "This item weighs " << kilo << " pounds.\n" << endl;
        break;
    case 2:
        cout << "Enter your weight in pounds: ";
        cin >> weight;
        pounds = weight * 2.2;
        cout << "This item weighs " << pounds << " kilos.\n" << endl;
        break;
    default:
        cout << "\nGoodbye!\n";
        return 0;
    }
}

int main()
{
    while (choice < 3)
    {
        displayMenu();
        getChoice(choice);
    }
    return 0;
}