c - Program crashes when using scanf into third Array declared -
for reason, able use scanf char array, , first numeric one, however, program crashes when input data next array, in case, withdrawal[wt]. please take @ code. taking intro class c, not 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 using matrix banking system. take red pill.\n\n"); /* prompt name */ printf ("please enter name: "); gets (name); printf ("\nhello %s.\n\n", name); /* prompt account balance, makes sure ammount 0 or more */ { printf ("now enter current balance in dollars , cents: $"); scanf ("%f", &balance); fflush(stdin); if ( balance < 0 ) printf ("error: beginning balance must @ least zero, please re-enter!\n\n"); /* error message negative balance amount */ } while ( balance <= 0 );/* end loop */ /* enter number of withdrawals, , number of deposits. must 0 or more */ { printf ("\nenter number of withdrawals: "); scanf ("%i", &number_of_withdrawals); fflush(stdin); if ( number_of_withdrawals < 0 || number_of_withdrawals > 50 ) printf ("number of withdrawals must @ least zero, no more 50.\nplease re-enter!\n\n"); /* error message negative number of withdrawals */ } while ( number_of_withdrawals < 0 || number_of_withdrawals > 50 );/* end loop */ { printf ("\nenter number of deposits: "); scanf ("%i", &number_of_deposits); fflush(stdin); if ( number_of_deposits < 0 || number_of_deposits > 50 ) printf ("number of deposits must @ least zero, no more 50.\nplease re-enter!\n\n"); /* error message negative number of deposits */ } while ( number_of_deposits < 0 || number_of_deposits > 50 );/* end loop */ printf ("\n\n\n"); /* spacing */ /* assign value deposits. must positive number. */ { printf ("enter amount of deposit #%i: ", x); scanf ("%f", &deposit[d]); if ( deposit[d] <= 0 ) printf ("error: deposit amount must more 0\n"); if ( deposit[d] <= 0 ) balance = balance - deposit[d]; else x++; balance = balance + deposit[d]; } while ( x <= number_of_deposits ); printf ("\n\n"); { if ( balance == 0 ) { printf ("you out of money in matrix, take blue pill."); break; } else printf ("enter 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 more 0\n"); balance = balance + withdrawal[wt]; } else y++; } while ( y <= number_of_withdrawals ); getchar();/* pause output*/ return 0; } /*end main */
you have not initialized variable wt
anywhere in program. hence program crashes here
scanf ("%f", &withdrawal[wt]);
initialize variable. not handling incrementing part of wt
anywhere in code. may need alter that
Comments
Post a Comment