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

问题描述 投票:0回答:1

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

    /*
    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 << choice; 移到很多不同的地方,包括为开关写的while循环里面,但我似乎只能让它显示菜单的提示,但不能显示菜单选项本身,而且它不接受输入。如果有区别的话,我是第一次使用Xcode,我需要它在那里运行,因为我不能在我的Visual studio(Mac)版本上运行C++。我可能在哪里出了问题,任何关于为什么我的版本是错误的见解都会被感激,因为我仍然是非常新的编码整体。谢谢你。

c++ xcode loops menu switch-statement
1个回答
0
投票

你有这个。

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
   }
© www.soinside.com 2019 - 2024. All rights reserved.