C ++基本菜单驱动程序

时间:2014-02-03 03:56:10

标签: c++

我有一个问题应该很简单,但我无法在任何地方找到答案。

我有一个菜单驱动的C ++程序,当菜单选项是数字时,它可以很好地工作,但我无法弄清楚如何将数字更改为字母。

例如:

当选择为1,2或3但 B,C

时,

工作正常

不确定我应该如何宣布该选项...它是一个字母吗?提前谢谢

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    //Variables needed for the problem
    int numbServCharge; //number of service charges
    char choice; //menu choice
    double transAmt, balence, totalServCharge; //transaction amount, current balence, total service charges

    //Constant service change
    const double servCharge = 0.25; //constant $0.25 for service charges

    numbServCharge = 0; //Start the counter for number of service charges at zero
    cout << "Checkbook Balencing Program\n\n";
    cout << "Enter the beginning balence:  "; 
    cin >> balence; //get the initial balence
    cout << endl;

    do
    {
        //Highlight menu options
        cout << "\nCommands\n";
        cout << "C - process a check\n";
        cout << "D - process a deposit\n";
        cout << "E - end the program\n\n";

        //Get user's choice from menu
        cout << "Enter transaction type:  ";
        cin >> choice;
        cout << endl;

        //Create an error message for invalid choice and get a second choice
        while((choice != 'C') && (choice != 'D') && (choice != 'E')
        {
            cout << "Invalid selection.  Please choose C, D or E: ";
            cin >> choice;
        }

        //Set up for option #1 -- using a check
        if(choice=='C')
        {
            cout << "Enter transaction amount:  ";
            cin >> transAmt; //Get the amount of the check
            cout << "\nProcessing check for $" << fixed << setprecision(2) << transAmt;
            transAmt = transAmt + servCharge; //Add the service charge onto the transaction
            numbServCharge++; //Add one to the number of service charges there have been
            balence = balence - transAmt; //Update account balence
            cout << "\nBalence:  $" << fixed << setprecision(2) << balence;
            cout << "\nService charge:  $0.25 for a check\n";
            totalServCharge = servCharge * numbServCharge; //Update total cost of service charges
            cout << "Total service charges:  $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
        }

        //Set up for option #2 -- deposits
        if(choice=='D')
        {
            cout << "Enter transaction amout:  ";
            cin >> transAmt; //Get the amount of the deposit
            cout << "\nProcessing Deposit for $" << fixed << setprecision(2) << transAmt << endl;
            transAmt = transAmt - servCharge; //Add the service charge onto the deposit
            numbServCharge++; //Add one to the number of service charges there have been
            balence = balence + transAmt; //Update account balence
            cout << "Balence:  $" << fixed << setprecision(2) << balence << endl;
            totalServCharge = servCharge * numbServCharge; //Update total cost of service charges
            cout << "Total service charges:  $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
    }

    }while(choice != 'E'); //Close program if option 3 is selected

    //Display final balence
    cout << "Processing end of the month";
    cout << "\nFinal balence :  $  " << fixed << setprecision(2) << balence << endl << endl;

    system("pause"); //Pause screen so it doesn't close right away
    return 0;
}

2 个答案:

答案 0 :(得分:1)

测试字符串时,应将所有内容转换为常见情况。在您的情况下,您应该将用户输入转换为大写。您可以使用toupper函数

执行此操作

以下是您需要更改以使程序正常工作的代码

cout << "Enter transaction type:  ";
cin >> choice;
choice = toupper(choice); // Change Here
cout << endl;

答案 1 :(得分:0)

while((choice != 'C') && (choice != 'D') && (choice != 'E')更改为while((choice != 'C') && (choice != 'D') && (choice != 'E'))后,您的代码运行良好。虽然我个人使用的是std::string而不是char

当然,凯撒的答案是有效的 - 我也会改变它。但是,作为评论会更好,因为它不能解决问题。等等,坚持下去。什么问题?即使使用字母菜单选项,您的程序似乎仍然“完美运行”。

程序中唯一的错误是当您尝试将balence(或三个double中的任何一个)分配给非数字条目时。当我输入“C”作为初始余额时,我看到了这种怪异:

That's interesting.

一遍又一遍。那不好玩。如果我为交易金额键入一封信,也会发生类似的事情:

Also interesting.

解决方案:切勿尝试将直接从用户处获取的输入阻塞到不是字符串类型的变量中。使用内置的字符串到数字函数,如atof(更喜欢)添加错误处理程序,如下所示:

if (std::cin >> dbl)
{
    //input is a double. handle that here.
}
else
{
    //input isn't a double. handle that here.
}

顺便说一句,它是拼写平衡,而不是balence;)