出于某种原因,我能够将scanf用于我的char数组,并且第一个数字,但是,当我输入下一个数组的数据时,程序总是崩溃,在这种情况下,撤销[wt]。请看一下代码。我正在上C级的介绍课程,所以我显然不是专家用户。
#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 */
答案 0 :(得分:1)
您尚未在程序中的任何位置初始化变量wt
。因此程序崩溃了
scanf(&#34;%f&#34;,&amp;撤回[wt]);
初始化变量。此外,您不会在代码中的任何位置处理wt
的递增部分。您可能需要更改