无法获取菜单选择以运行函数.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: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;
}
- 您确定要在这里使用while循环吗
while (choice == 1)
{
double kilosToPounds(weight);
}
- 如果choice==1,您将如何以及何时退出循环
此外,您可能需要对您正在计算的值执行一些操作:
double weightInPounds = kilosToPounds(weight);
您的代码中有几个错误:
- 如果您已经有接受用户输入的
getChoice
函数,为什么要在调用getChoice
函数之前接受用户输入
cout << "Please choose a function: ";
cin >> choice; // <-- not needed
- 如果只想检查某个条件,为什么要使用
while
循环?请改用if
语句
if (choice == 1) {...}
而不是
while (choice == 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;
}
相关文章:
- 无法获取菜单选择以运行函数.C++
- 为什么std::async使用同一个线程运行函数
- MINGW - 正确运行函数所需的 cdecl
- 如何在 C 中使用空的 main() 方法运行函数?
- 运行函数作为 constexpr 和不作为 constexpr
- 如何在全局变量的构造函数之前运行函数
- 在 Cap'n Proto RPC 服务器中定期运行函数
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- CLang:在 std::thread 中运行函数会导致结构创建BAD_ACCESS
- 如何使用 TBB 在单个线程中运行函数
- 我在运行函数 GetVolumeInformation() 时得到非常随机的结果
- 如何使用 boost::asio io_service 运行函数异步
- Qt - 在其他线程上运行函数
- 在一组模板化对象上运行函数
- pybind11:属性错误:尝试从 py 文件运行函数时,模块'XXX'没有属性'YYY'
- 添加按钮以通知运行函数的通知
- 如何在程序后台运行函数(特别是自动保存函数)?QT / C++
- Qt - 在单独的线程中运行函数
- 如何在单独的线程上运行函数(如果线程可用)
- 如何发出编译和运行C++函数的 LLVM IR