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(在 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
}
相关文章:
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- Win32编译器选项和内存分配
- 无法获取菜单选择以运行函数.C++
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 通过选项卡的文本设置QTabWidget顺序
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- Winapi创建选项卡菜单
- 使用WXWIDGET C 将菜单和子菜单选项添加到Mac中的窗口菜单
- 在菜单前键入字符串后,C 如何从菜单中选择选项
- 如何使用基于 IExecuteCommand 的 shell 扩展将某些上下文菜单选项变灰
- Mac OSX 上的 Eclipse CDT 未在首选项集中显示调试菜单
- IFstream在选择菜单选项后无法打开文件
- 采用过滤后的用户输入,并使其与菜单选项相对应
- QT-菜单的退出选项
- 查找 wx菜单的选定单选项
- c++ Windows在菜单中添加选项
- 用c++编写一个菜单程序,让用户从选项列表中进行选择,如果输入的不是其中一个选项,则重新打印该列表
- C++循环一个菜单,直到选择了所有选项