在函数c++中调用函数

calling a function in a function c++

我好像想不通。我想做的是声明三个函数。接受用户输入值 "balance" 的一种。另一个显示菜单。我想在 "main" 函数中调用这些函数,并希望从函数 "balanceAcquired" 中输入 "balance"。

我是 C++ 的新手,所以我的代码可能草率,但我遇到的错误是

error C4716: 'balanceAcquire': 必须 return 一个值

error C4716: 'menu' : 必须 return 一个值

错误 c4700:使用了未初始化的局部变量 'balance'

// check book balancing program

//header files
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>

using namespace std;


int balanceAcquire(){

    float balance;          // balance entered by user

    // acquire balance

    cout << "check book balancing program \n\n";
    cout << "Enter the beginning balance: ";
    cin >> balance;
}



int menu(){

//menu

    cout << "Commands: \n";                     // commands
    cout << "C - process a check\n";            // process a check
    cout << "D - process a deposit\n";          // process a deposit
    cout << "E - End the program\n";            // end the program
}


int main(){

// define variables

    const float fee = .25;          // fee for transaction

    float check,                    // check amount
        deposit,                    // deposit amount
        balance;                    // balance amount

    char choice;                    // user entered choice
    float serviceCharges = 0;       // service charges amount tally

// initiate program

    balanceAcquire();
    menu();

    cout << "Enter transaction type: " << endl;     // ask for a choice
    cin >> choice;                                  // choice type

    switch (choice) {
        case 'c':
            cout << "Enter transaction amount: " << endl;   
            cin >> check;                           
            cout << "Processing check for" << check << endl;                        
            balance -= check; 
            cout << "Balance: " << setprecision(2) << balance << endl;
            cout << "Service charge: $.25 for a check" << endl;                     
            serviceCharges += fee;                                                  
            cout << "Total service charges: " << serviceCharges << "\n\n";
            break;                                                                  

        case 'd':
            cout << "Enter transaction amount: " << endl;                           
            cin >> deposit;                                                         
            cout << "Processing deposit for" << deposit << endl;                    
            balance += deposit;                                                     
            cout << "Balance: " << setprecision(2) << balance << endl;              
            cout << "Total service charges: " << serviceCharges << "\n\n";  
            break;

        case 'e':
            cout << "Processing end of month" << endl;
            balance -= serviceCharges;
            cout << "Final Balance: $" << balance << endl;
            break;
    }
}

除其他外,我尝试以这种方式写出来的全部目的是这样我可以 return 到 "menu" 允许用户 select 更多存款或支票输入并在最后收到最终余额减去交易费用。

编辑 1:格式化

跟着错误走就行了。

error C4716: 'balanceAcquire': must return a value

int balanceAcquire() {
*** <== declared to return int, no return in body

error C4716: 'menu' : must return a value

int menu() {
*** <== declared to return int, no return in body

error c4700: uninitialized local variable 'balance' used

在您的主函数中,您的代码如下所示:

float balance;
// [ snip ]
balance -= check; 
balance += deposit; 
balance -= serviceCharges;

这是未定义的行为,您必须在读取之前将 balance 初始化为某个值。可能您想将其分配给 balanceAcquire() 的 return 值,他们应该 return a float:

balance = balanceAcquire();

通过阅读您的代码,我得出的结论是您没有掌握函数调用的工作原理。一言以蔽之:

函数体只能与其签名中存在的内容以及全局状态进行交互。他们不会从调用者那里隐式继承任何东西。

比如你想让balanceAcquire()影响"outside",即main(),你需要自己接管。

第一种方式:

int balanceAcquire() {
    float b;
    //...
    cin >> b;
    return b;
}

函数returns值。在外部,您可以通过使用函数调用表达式作为值来取回它:

int main() {
    float balance;
    //...
    balance = balanceAcquire();
    // balance is set.
}

第二种方式:

void balanceAcquire(float &b) {
    //...
    cin >> b;
}

函数returns什么都没有(因此void),但是通过引用(&)传入b,这意味着它可以直接访问来自外部的变量。

int main() {
    float balance;
    //...
    balanceAcquire(balance);
    // balance is set.
}