mercredi 4 octobre 2017

ERROR: Variable is reinitialised while using passing by reference C++

#include "stdafx.h"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::exit;

void pin(double &Balance);
void menu();
void helpMenu();
void withdraw();
void deposit();
void overdraft();
void viewBalance();

int main()
{   
    double Balance;
    pin(Balance);
    return 0;
}

void pin(double &Balance)
{
    int AccountPin = 8376;
    int AttemptNum = 0;
    int Pin;

    double balance = 1000;

    do
    {
        cout << "Enter your pin: ";
        cin >> Pin;
        if (Pin == AccountPin)
        {
            menu();
        } 
        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;
    exit(EXIT_FAILURE);
}

void menu()
{
    int choice;

    do {
        cout << "Please choose from these options:\n1: Withdraw\n2: Deposit\n3: View balance\n4: Apply for an Overdraft\n5: Help menu\n6: Exit\nPlease choose between 1 and 6: ";
        cin >> choice;

        switch (choice)
        {
        case 1:
            withdraw();
            break;
        case 2:
            deposit();
            break;
        case 3:
            viewBalance();
            break;
        case 4:
            overdraft();
            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 £200\nIf you have nothing left in your account you can apply for a overdraft" << endl << endl;
    menu();
}

void withdraw()
{   
    double withdraw;
    double Balance;
    cout << "How much would you like to withdraw: ";
    cin >> withdraw;
    pin(Balance);
    Balance = Balance - withdraw;
    if (Balance < 0) {
        cout << "You need to apply for an overdraft" << endl;
        Balance = Balance + withdraw;
    }
    cout << Balance;
    menu();

}

void deposit()
{
    double deposit;
    double Balance;
    cout << "How much you like to deposit: ";
    cin >> deposit;
    pin(Balance);
    Balance = Balance + deposit;
    menu();
}

void overdraft()
{

}

void viewBalance()
{
    double Balance;
    pin(Balance);
    cout << "You have " << char(156) << Balance <<  " pounds in your bank account" << endl;
    menu();
}

I am creating a ATM program and I need to use parameter passing to get the balance value and deposit, withdraw and apply for overdraft. however when I run the code and then deposit the balance doesn't increase. also It repeats it self due to where I put the variable balance I'm actively looking for a location that will work and though my pin procedure would work but it didn't.

Aucun commentaire:

Enregistrer un commentaire