【问题标题】:C++ basic menu driven program (calling functions)C++基本菜单驱动程序(调用函数)
【发布时间】:2014-02-10 00:42:09
【问题描述】:

我是编程新手,我想弄清楚为什么会这样。

基本上,我被要求设计一个菜单驱动的程序来进行一些基本的存款/取款计算。我应该为每个进程编写不同的函数,并在必要时调用它们。

我有两个问题: 1)我的函数没有更新程序中的变量。例如它会运行,我可以输入我的起始余额,我可以输入我的交易类型和金额,但是每次我从存款切换到写支票时,它都会将余额重置为原始用户输入。

2) 当我想让程序退出时,它仍然要求我输入双重输入。不知道如何让它只接受一个“E”而不是“E number”

提前致谢。

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

//Prototypes for my functions that will be called in
void displayMenu();
double checkProcess(double, double);
double depositProcess(double, double);
double totalServCharge(double);

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

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

    do
    {
        //Call the display menu function
        displayMenu();

        //Get user's choice and transaction amount from menu
        cout << "Enter a transaction:  ";
        cin >> choice >> transAmt;
        choice = toupper(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')
        {
        //Call check function and service charges function
        checkProcess(transAmt, balance);
        totalServCharge(numbServCharge);
        }

        //Set up for option #2 -- deposits
        if(choice=='D')
        {
        //Call deposit function and service charges function
        depositProcess(transAmt, balance);
        totalServCharge(numbServCharge);
        }

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

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

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

void displayMenu()
{
//Highlight menu options
cout << "\nCommands\n";
cout << "C amount - process a check in a specific amount\n";
cout << "D amount - process a deposit in a specific amount\n";
cout << "E - end the program\n\n";
}

double checkProcess(double transAmt, double balance)
{
cout << "\nProcessing check for $" << fixed << setprecision(2) << transAmt;
transAmt = transAmt + .25; //Add the service charge onto the transaction
balance = balance - transAmt; //Update account balence
cout << "\nBalance:  $" << fixed << setprecision(2) << balance;
cout << "\nService charge:  $0.25 for a check\n";
return balance;
}

double depositProcess(double transAmt, double balance)
{
cout << "\nProcessing Deposit for $" << fixed << setprecision(2) << transAmt << endl;
transAmt = transAmt - 0.25; //Add the service charge onto the deposit
balance = balance + transAmt; //Update account balence
cout << "Balance:  $" << fixed << setprecision(2) << balance << endl;
return balance;
}

double totalServCharge(double numbServCharge)
{
double totalServCharge = 0;
numbServCharge++; //Add one to the number of service charges there have been
totalServCharge = .25 * numbServCharge; //Update total cost of service charges
cout << "Total service charges:  $" << fixed << setprecision(2) << totalServCharge << endl; //Tell user total service charges so far
return numbServCharge;
}

【问题讨论】:

    标签: c++


    【解决方案1】:

    (1) 你的变量没有更新,因为你没有改变它们;您正在更改传递给子例程的副本。您要么需要通过引用传递当前余额(这样当您在子例程范围内更改它时,您也会更改原始余额)或将子例程的返回值分配给您的当前余额。

    当您按值传递变量时(就像现在一样),程序会复制该变量以在函数范围内使用。当您离开该范围时,副本将被删除。当您想确保子例程不会更改调用它的范围内的任何内容时,请执行此操作。如果您希望子例程更改您传递给它的变量,请通过引用传递变量。在这个函数签名中,“transAmt”通过值传递,“balance”通过引用传递:

    double foo(double transAmt, double& balance);
    

    顺便说一句,如果您通过引用传递余额,则没有理由退回它。无论如何,您现在没有对返回的值做任何事情。

    (2) 如果您不想在给出“E”时询问数字,请为“C”和“D”的条件块中的数字分别制作“cin”语句。现在,您的所有输入代码都在所有案例之间共享。如果你安排一些事情,以便在要求一个数字之前退出循环,那就更好了。这样,您只需编写一次“cin”语句。

    【讨论】:

    • 谢谢...一旦我将第二个 cin 语句放入两个 if 语句中,它就可以正常工作了。
    【解决方案2】:

    这是因为在 C++ 中,当您调用带参数的函数时,您只能对这些参数的副本进行操作。如果要更改原始值,请将变量作为引用传递。

    看看那些简单的例子:

    #include <iostream>
    
    using namespace std;
    
    void normal(int a)
    {
        a += 10;
    }
    
    void by_reference(int &a) //Notice ampersand
    {
        a += 10;
    }
    
    int by_return(int a)
    {
        return a + 10;
    }
    
    int main()
    {
        int a = 5;
    
        cout << "Start:     " << a <<endl;
        normal(a);
        cout << "Normal:    " << a <<endl;
        by_reference(a);
        cout << "Reference: " << a <<endl;
        a = by_return(a);
        cout << "Return:    " << a <<endl;
    }
    

    现在您可以看到,如果您将变量作为引用传递,它会发生变化。

    即使只是将 & 放在参数名称之前就可以解决您的问题,我还是建议您使用 return,它通常更简单并且使您的代码看起来更好(更容易理解)。

    另外,更正你的缩进。当您处理更大的项目时,这一点非常重要。

    希望这会有所帮助。 :)

    【讨论】:

    • 谢谢!缩进很有趣,因为这个网站要求代码。感谢回复
    【解决方案3】:

    1 |出现第一个问题是因为您实际上并未修改用户的输入。您正在传递变量,但是一旦返回新值,您就不会存储它,因此主函数中会忘记该值。不是最好的解决方案,但它可能适合您这样做:

        if (choice == 'D')
        {
            //Call deposit function and service charges function
            balance = depositProcess(transAmt, balance); // Overwrite balance
            numbServCharge = totalServCharge(numbServCharge); // Overwrite numbServCharge
        }
    

    或者以更有效的方式,您可以通过引用传入变量。您需要做的就是更改函数原型。

    void totalServCharge(double&);
    void depositProcess(double&, double&);
    

    现在当变量传入时,会直接被函数修改。另请注意,函数数据类型已更改为 void,因为您不再需要从函数返回值。

    2 |您的第二个问题是您处理用户输入的方式。如果您想请求多个输入,有时最好使用单独的 cout 和 cin 语句来完成。 它在您的程序中设置的方式是让用户一次输入两个输入,因此如果您只输入 E,它会耐心等待您的下一个输入 (transAmt)。处理所需的操作然后输入金额可能会更好。

    非常一般的例子:

    do {
        cout << "1 : Transaction or 2 : Exit" << endl;
        cin >> choice;
        if (choice == 2)
            break;
    
        cout << "1. Deposit\n" << "2. Check" << endl;
        cout << "Enter transaction type followed by amount" << endl;
        cin >> transType >> amount;
        // Then do what was specified in choice
    } while (true); // Or whatever condition pleases you
    

    【讨论】:

    • 第一个任务就是这样,但是对于这个我们应该把它结合起来。不知道如何在他们都在同一行的地方做到这一点。
    • 我已经更新了我的上一个代码示例。我不确定这是否符合您的作业标准,但我制作了一个菜单,询问用户是否要进行交易,另一个询问类型和金额(全部在一个 cin 语句中)。
    • 当我取出第二个 cin 并将其移动到用于支票和存款的 if 语句中时,它运行良好。通过这样做,我认为它在需要第二个语句之前评估了第一个语句
    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多