-1

Hey guys I am constructing an ATM Program, and I have everything ok

I have the menu it pulls up you can select an option and it runs the function HOWEVER, I cannot for the life of me

set a balance and get it to stay until its changed and I need it to save once it has changed in one of the two options (deposit, withdrawl) since this is a post test loop it will keep going until exit is selected and every time I need this to update the balance.

Here is my C Code for it, if anyone could help that would be amazing.

#include <stdio.h>
#include <stdlib.h>

// Function Declarations

    int getChoice ();
    double withdraw (int Choice, int Balance);
    double deposit (int Choice, int Balance);
    int VBalance (int Choice, int Balance);
    double process (int Choice, int Balance);



int main (void)
{
// Local Declarations

    int Choice;
    int Balance;


// Statements

do
{
    Balance = 2500.00;
    Choice = getChoice ();
    process (Choice, Balance);
}
while (Choice != 0);


return 0;





} // Main

/*============================process=*/

double process (int Choice, int Balance)
{
// Declarations


// Statements
    switch(Choice)
        {
            case 1: withdraw (Choice, Balance);
                break;
            case 2: deposit (Choice, Balance);
                break;
            case 3: VBalance (Choice, Balance);
                break;
            case 0:  exit;
                break;
            deafult: printf("Sorry Option Not Offered");

} // switch

return 0;
}


/*============================getChoice=*/

int getChoice (void)
{
// Local Declarations
    char Choice;

// Statements

    printf("\n\n**********************************");
    printf("\n             MENU             ");
    printf("\n\t1.Withdrawl Money          ");
    printf("\n\t2.Deposit Money           ");
    printf("\n\t3.View Balance            ");
    printf("\n\t0.Exit                    ");
    printf("\n**********************************");
    printf("\nPlease Type Your Choice Using 0-3");
    printf("\nThen Hit Enter: ");
    scanf("%d", &Choice);

return Choice;

} //getchoice


/*============================withdraw=*/


double withdraw (int Choice, int Balance)
{
// Local Declarations
    double amount;


// Statements
    printf("Funds:%d", &Balance);
    printf("\nPlease Enter How Much You Would Like To Withdraw: ");
    scanf("%f", &amount);
    Balance = Balance - amount;

return Balance;

}  //withdraw


/*============================Deposit=*/

double deposit (int Choice, int Balance)
{
// Local Declarations
    double amount;

// Statements
    printf("Funds:%d", &Balance);
    printf("\nPlease Enter How Much You Would Like To Deposit: ");
    scanf("%f", &amount);
    Balance = Balance + amount;

return Balance;

} //Deposit


/*============================VBalance=*/

int VBalance (int Choice, int Balance)
{
// Statements
    printf("\nYour Current Funds:%d", &Balance); 
    printf("\nThank Your For Viewing");

return 0;
}
Tinwor
  • 7,765
  • 6
  • 35
  • 56

1 Answers1

0

First: Enable compiler warnings. If you use gcc, add -Wall to the command line. If you use an IDE the option to turn on warnings should be in the compiler settings.

When you compile with warnings you will find that you have some problems with your use of printf and scanf. Fix those!

Next you have the problem with Balance in main not getting updated. C uses call by value which means that the changes you make to a function argument are not visible in the calling function. To work around that you could declare the balance parameters as pointers and pass the address of Balance. Or you could return the new Balance, just like you do in your code. The only thing you forgot is to store the new value to Balance in main.

Changed line in main:

Balance = process(Choice, Balance);

Changed process:

double process(int Choice, int Balance)
{
    // Declarations

    // Statements
    switch (Choice)
        {
            case 1: Balance = withdraw(Choice, Balance); // Changed line
                break;
            case 2: Balance = deposit(Choice, Balance); // Changed line
                break;
            case 3: Balance = VBalance(Choice, Balance); // Changed line
                break;
            case 0:  exit;
                break;
            deafult: printf("Sorry Option Not Offered");

    } // switch

    return Balance;  // Changed line
}

Third: Balance is declared an int, but you sometimes use double. You should choose one type and stick to it. Due to the nature of floating point numbers it is actually recommended that an integral type is used for money.

Community
  • 1
  • 1
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82