gpt4 book ai didi

c - 使用 scanf 进入声明的第三个数组时程序崩溃

转载 作者:行者123 更新时间:2023-11-30 15:35:28 25 4
gpt4 key购买 nike

由于某种原因,我可以对我的字符数组和第一个数字数组使用 scanf,但是,当我输入下一个数组的数据时,程序总是崩溃,在本例中为withdrawal[wt]。请看一下代码。我正在参加 C 入门类(class),所以我显然不是专家用户。

#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 */

最佳答案

您尚未在程序中的任何位置初始化变量wt。因此程序在这里崩溃了

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

初始化变量。此外,您没有在代码中的任何地方处理 wt 的递增部分。您可能需要更改它

关于c - 使用 scanf 进入声明的第三个数组时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22899395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com