0

For some reason, I am able to use scanf for my char array, and the first numeric one, however, the program always crashes when I input the data for the next array, in this case, withdrawal[wt]. Please take a look at the code. I am taking an intro class to C, so I am obviously not an expert user.

#include <stdio.h>

int main(void)
{
    /* Declare Variables */
    float withdrawal[50] = {0}, deposit[50] = {0};
    char name[50];
    int number_of_deposits, number_of_withdrawals, x = 1, y = 1, d, wt;
    float balance;

    /* Welcome message */
    printf ("Thank you for using The Matrix banking system. Where you always take the red pill.\n\n");

    /* Prompt for name */
    printf ("Please enter your name: ");
    gets (name);
    printf ("\nHello %s.\n\n", name);

    /* Prompt for account balance, makes sure ammount is 0 or more */
    do
    {
        printf ("Now enter your current balance in dollars and cents: $");
        scanf ("%f", &balance);
        fflush(stdin);

        if ( balance < 0 )
            printf ("Error: Beginning balance must be at least zero, please re-enter!\n\n"); /* Error message for negative balance amount */

    } while ( balance <= 0 );/* end loop */

    /* Enter the number of withdrawals, and number of deposits. Must be 0 or more */

    do
    {
        printf ("\nEnter the number of withdrawals: ");
        scanf ("%i", &number_of_withdrawals);
        fflush(stdin);

        if ( number_of_withdrawals < 0 || number_of_withdrawals > 50 )
        printf ("Number of withdrawals must be at least zero, but no more than 50.\nPlease re-enter!\n\n"); /* Error message for negative number of withdrawals */

    } while ( number_of_withdrawals < 0 || number_of_withdrawals > 50 );/* end loop */

    do
    {
        printf ("\nEnter the number of deposits: ");
        scanf ("%i", &number_of_deposits);
        fflush(stdin);

        if ( number_of_deposits < 0 || number_of_deposits > 50 )
        printf ("Number of deposits must be at least zero, but no more than 50.\nPlease re-enter!\n\n"); /* Error message for negative number of deposits */

    } while ( number_of_deposits < 0 || number_of_deposits > 50 );/* end loop */

    printf ("\n\n\n"); /* for spacing */

    /* Assign value to deposits. Must be positive number. */

    do
    { 
        printf ("Enter the amount of deposit #%i: ", x);
        scanf ("%f", &deposit[d]);

        if ( deposit[d] <= 0 )
        printf ("Error: Deposit amount must be more than 0\n");
        if ( deposit[d] <= 0 )
        balance = balance - deposit[d];
        else
        x++;
        balance = balance + deposit[d];

    } while ( x <= number_of_deposits );

    printf ("\n\n");

    do
    { 
        if ( balance == 0 )
        {
            printf ("You are out of money in The Matrix, take the blue pill.");
            break;
        }

        else    
            printf ("Enter the amount of withdrawals #%i: ", y);
            scanf ("%f", &withdrawal[wt]);
            balance = balance - withdrawal[wt];

            if ( balance < 0 )
            {
                printf ("***Withdrawal amount exceeds current balance.***\n");
                y--;
                balance = balance + withdrawal[wt];
            }
            else if ( withdrawal[wt] <= 0 )
                {
                printf ("Error: Withdrawal amount must be more than 0\n");
                balance = balance + withdrawal[wt];
                }
                else
                y++;

    } while ( y <= number_of_withdrawals );


    getchar();/* to pause output*/

    return 0;
} /*End main */
2
  • 1
    I do hope that you are not writing the software for my bank :-) d is not initialized in scanf ("%f", &deposit[d]). wt is not initialized in scanf ("%f", &withdrawal[wt]). gets() is a completely unsafe function. fflush(stdin) is undefined behaviour (unless perhaps on Linux) ... Commented Apr 6, 2014 at 20:21
  • I realize now that I was just assigning new values to the same array subscript, because I added variables to the loop, as opposed to increasing the counter inside the array. Thanks for the answers though. Commented Apr 6, 2014 at 21:27

1 Answer 1

1

You have not initialized the variable wt anywhere in the program. Hence the program crashes here

scanf ("%f", &withdrawal[wt]);

Initialize the variable. Also you are not handling the incrementing part of wt anywhere in the code. You may need to alter that

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.