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 */
dis not initialized inscanf ("%f", &deposit[d]).wtis not initialized inscanf ("%f", &withdrawal[wt]).gets()is a completely unsafe function.fflush(stdin)is undefined behaviour (unless perhaps on Linux) ...