无法获取菜单选择以运行函数.C++

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

本文关键字:运行 函数 C++ 选择 获取 菜单      更新时间:2023-10-16

我想创建一个代码,从菜单选择中调用一个函数。我希望菜单能选择换算公斤和磅的动作。我当前代码中的菜单选择无法完成此任务。如何使菜单选择生效?我希望它将公斤转换为磅,将磅转换为公斤,然后退出该程序。菜单选择应循环选择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:nn"
<< right
<< setw(padding) << "" << "1. Convert kilograms to poundsn"
<< setw(padding) << "" << "2. Convert pounds to kilogramsn"
<< setw(padding) << "" << "3. Quitn";
}

/*****     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. 调用函数时,不需要指定其返回类型
auto pounds = kilosToPounds(weight);

或者只是

kilosToPounds(weight);

而不是

double kilosToPounds(weight);

演示

见下文,

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循环中没有输入,因此,如果您在小于min或大于max的时间内输入choice,则循环将无限,因为choice变量没有变化。所以在循环中输入。

在主((中

如果选择等于1或2,则由于而进入无限循环

while (choice == 1)..

和In 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:nn"
<< 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:nn"
<< right
<< setw(padding) << "" << "1. Convert kilograms to pounds" << endl
<< setw(padding) << "" << "2. Convert pounds to kilograms" << endl
<< setw(padding) << "" << "3. Quitn" << 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;
}