C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?

C++ The loop in my switch format isn't allowing me to display menu options or accept input?

本文关键字:选项 菜单 输入 显示 允许我 格式 开关 我的 不允许 循环 C++      更新时间:2023-10-16

我需要为一门课程编写一个贷款程序,到目前为止我有这段代码:

/*
Project: Course Project - Loan Calculator
Program Description: To calculate customer's loan
information including interest rate, monthly
payment and number of months payments will be
required for.
Developer: ___________
Last Update Date: June 1st, 2020
*/
#include <iostream>
#include <string>

//variables listed here
double loanAmt = 0, intRate = 0, loanLength = 0;
std::string CustName[0];
int choice; //menu choice identifier

using namespace std;
int main()
{
//Welcome User
cout << "Thank you for using our calculator." << endl;
cout << endl;
cout << "Please enter the number of your preferred calculator and press 'Enter': " << endl;
cout << endl;
cin >> choice;
{
while (choice !=4);
switch (choice)
{
case 1:
cout << "Monthly Payment Calculator:";
cout << endl;
break;
case 2:
cout << "Interest Rate Calculator:";
cout << endl;
break;
case 3:
cout << "Payment Term (In Months):";
cout << endl;
case 4:
cout << "Exit Calculator:";
default: //all other choices
cout << "Please Select A Valid Option.";
break;
}
}
}

我尝试将 cin <<选择移动到许多不同的位置,包括为开关编写的 while 循环内部,但我似乎只能让它显示菜单提示,而不是菜单选项本身,它不接受输入。如果它有所作为,我是第一次使用 Xcode,我需要它在那里运行,因为我无法在我的 Visual studio(在 Mac 上(上运行C++。我在这里可能哪里出错了,任何关于为什么我的版本错误的见解都是值得赞赏的,因为我对整体编码仍然很陌生。谢谢。

你有这个:

while (choice !=4);

这使得你的代码永远不会继续超过这个while循环。除非编译器在优化期间删除此行。

这是正确的方法:

while (choice !=4) {
switch (choice)
{
case 1:
cout << "Monthly Payment Calculator:";
cout << endl;
break;
case 2:
cout << "Interest Rate Calculator:";
cout << endl;
break;
case 3:
cout << "Payment Term (In Months):";
cout << endl;
case 4:
cout << "Exit Calculator:";
default: //all other choices
cout << "Please Select A Valid Option.";
break;
}
cin >> choice; // have it right after the switch
}