错误:如何通过参数传递来解决此问题

ERROR: How could i fix this issue with parameter passing?

本文关键字:解决 问题 参数传递 何通过 错误      更新时间:2023-10-16
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::exit;
using std::string;
void pin(double &Balance);
void menu(double &Balance);
void helpMenu();
void withdraw(double &Balance);
void deposit(double &Balance);
void overdraft(double &Balance);
void overdraftMenu(double &Balance);
void viewBalance(double &Balance);
void BuisnessOverdraft(double &Balance);
void OverFiveOverdraft(double &Balance);
void UnderFiveOverdraft(double &Balance);

int main()
{   
    double Balance;
    pin(Balance);
    return 0;
}
void pin(double &Balance)
{
    int AccountPin = 8376;
    int AttemptNum = 0;
    int Pin;
    do
    {
        cout << "Enter your pin: ";
        cin >> Pin;
        if (Pin == AccountPin)
        {
            menu(Balance);
        } 
        else
        {
            cout << "Wrong pin, please try again." << endl;
            AttemptNum++;
        }
    } while (AttemptNum < 3);
    cout << "You have used all of your attempts, you can no longer attempt with this card" << endl;
    return;
}
void menu(double &Balance)
{
    int choice;
    do {
        cout << "Please choose from these options:n1: Withdrawn2: Depositn3: View balancen4: Apply for an Overdraftn5: Help menun6: ExitnPlease choose between 1 and 6: ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            withdraw(Balance);
            break;
        case 2:
            deposit(Balance);
            break;
        case 3:
            viewBalance(Balance);
            break;
        case 4:
            overdraftMenu(Balance);
            break;
        case 5:
            helpMenu();
            break;
        case 6:
            exit(EXIT_FAILURE);
            break;
        default:
            cout << "Please try again" << endl;
            break;
        }
    } while (choice < 0 || choice > 6);
}
void helpMenu()
{
    cout << endl << "When prompted by the interface please input a number between the numbers it says.nYou cannot withdraw more than £200nIf you have nothing left in your account you can apply for a overdraft" << endl << endl;
    return;
}
void withdraw(double &Balance)
{   
    double withdraw;
    cout << "How much would you like to withdraw: ";
    cin >> withdraw;
    menu(Balance);
    Balance = Balance - withdraw;
    if (Balance < 0) {
        cout << "You need to apply for an overdraft" << endl;
        Balance =- withdraw;
    }
    return;
}
void deposit(double &Balance)
{
    double deposit;
    cout << "How much you like to deposit: ";
    cin >> deposit;
    menu(Balance);
    Balance =+ deposit;
    return;
}
void viewBalance(double &Balance)
{
    menu(Balance);
    double balance = Balance;
    cout << "You have " << char(156) << balance <<  " in your bank account" << endl;
    return;
}
void overdraftMenu(double &Balance)
{
    char choice;
    do {
        cout << "Would you like to apply for a overdraft, yes or no? ";
        cin >> choice;
        tolower(choice);
        if (choice == 'y') {
            overdraft(Balance);
        }
        else if (choice == 'n') {
            return;
        }
        else cout << "Please try again";
    } while (choice != 'y' || choice != 'n');
}
void overdraft(double &Balance)
{
    int choice;
    do {
        cout << "Are you:n1: A buisness ownern2: A customer of over 5 yearsn3: A customer of under 5 yearsnPlease enter a number between 1 and 3";
        cin >> choice;
        switch (choice)
        {
        case 1:
            BuisnessOverdraft(Balance);
            break;
        case 2:
            OverFiveOverdraft(Balance);
            break;
        case 3:
            UnderFiveOverdraft(Balance);
            break;
        default:
            cout << "Input a correct valuenPlease try again";
            break;
        }
    } while (choice < 1 || choice > 3);
}

void BuisnessOverdraft(double &Balance)
{
}
void OverFiveOverdraft(double &Balance)
{
}
void UnderFiveOverdraft(double &Balance)
{
}

我正在为学校工作创建一个ATM计划,我需要使用参数通过来获取资产价值和存款,撤回并申请透支。但是,当我运行代码然后存入余额时,余额不会增加。我还需要添加一个用于保存用户详细信息的系统,并且帐户详细信息将需要保存到文本文件中。

您需要在此处进行一些更正。首先,您使用的是函数本地局部的本地变量,一旦控制从函数返回,此局部变量值将不再可用。首先了解全局变量和局部变量。更好的是,您应该使用

之类的结构
          customer{
                      char customerName[200];
                  double balance = 0;
                  };

如果您不知道结构声明并使用它更好地学习。

我只是在修改您的代码以临时修复。

 double Balance = 1000.00; 
 int main()
 {
 }
 //declare Balance as global variable just above your main function.
 //modify your balance method like below
 void balance(double & balance)
 {
    balance = Balance;
 }
void deposit()
{
    double deposit;
    //double balance;//this is your local variable
    cout << "How much you like to deposit: ";
    cin >> deposit;
    //balance(balance);
    //Balance = balance + deposit; 
    //here in above Balance is your global variable   
    //better use a setBalance() method like below
    setBalance(deposit);
    menu();
}
void setBalance(double deposit)
{
    Balance = Balance + balance;
}
//This is your new method

这将解决您的问题。但是它仍然没有达到标记。关于在文件中存储值,只需了解如何在C 或C中创建和读取文件。首先自己尝试。如果您面临任何麻烦,我们可以为您提供帮助。

祝你好运!

您的代码中有几个主要缺陷:

  • 您不应在每个功能的末尾调用menu() - 这会导致递归。仅来自功能的returndo while循环将继续迭代。

  • 您在menu中的环路的状况错误。您可以在case 6上执行while(true)return,也可以进行while (choice != 6)。您不需要在那里进行exit(EXIT_FAILURE)。这样,它将返回控制到main并正确退出程序。

  • 请勿在功能中本地声明Balance。仅声明一次(menu看起来像个好地方),并将其传递给需要它的每个功能。

问题是,您将平衡定义为局部变量。这意味着它在函数body =>外部被销毁。

是无效的。

最简单的解决方案 - 如果可以接受,如果我正确理解您的问题 - 是您将其全局(当然可以删除本地定义)。

全局不可能:

然后,您需要通过参考将平衡传递给每个弹性。功能叛逃的第一个示例:

void pin(double& Balance){ .... }

称其为:

int main()
{
   double Balance;
   ...
   pin(Balance);
   ...
}

从PIN您的调用菜单中,您必须对菜单()进行相同的操作:

void menu(double& Balance) { ... }

并以相同的方式修改您从菜单调用的每个功能。

下一个删除

的所有本地定义
double Balance;

!!但是!! - 仅留在main()