C++ 开关格式中的循环不允许我显示菜单选项或接受输入?
C++ The loop in my switch format isn't allowing me to display menu options or accept input?
我需要为一门课程编写一个贷款程序,到目前为止我有这段代码:
/*
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 版本不能 运行 C++ (在 Mac 上)。我在这里哪里可能出错了,我对我的版本为什么错误的任何见解都表示赞赏,因为我对整体编码仍然很陌生。谢谢。
你有这个:
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
}
我需要为一门课程编写一个贷款程序,到目前为止我有这段代码:
/*
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 版本不能 运行 C++ (在 Mac 上)。我在这里哪里可能出错了,我对我的版本为什么错误的任何见解都表示赞赏,因为我对整体编码仍然很陌生。谢谢。
你有这个:
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
}