gpt4 book ai didi

c - While 循环在第一个循环后不接收数据

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

我的代码有点问题。我在这里所做的基本上是使用 ASCII 进行输入验证,并且在没有 while 循环的情况下它可以正常工作,但是如果我使用 while 循环,它将完美运行一次,但是当它返回到代码顶部时,它会停止工作这意味着它假设所有内容都是无效输入。

    void menu()
{
char userInput[10] = {0};
int asciiCode[10];
int i;
int countZero = 0;
int storeUserOption;

while (storeUserOption !=1 && storeUserOption !=2 && storeUserOption !=0)
{

printf("\nPlease enter any of the following options:\n");
printf("\nEnter winning number (w)\n");
printf("\nEnter ticket numbers (t)\n");
printf("\nquit (q)\n");
printf("\n>: ");

fgets(userInput, 10, stdin);

/*It converts whatever I type in asciiData, and see if there's left over data*/
for (i = 0; i < 10; i++)
{
asciiCode[i] = userInput[i];
if (asciiCode[i] == 0)
{
countZero++;
}
}


/*It takes left over data and subtracts it by 10 to reSize it*/
int reSize = (10 - countZero);

/*It checks to see if you enetered anything more than 2 element if so it will input
invalid input. For example can input dd, but can't input asdadasdasd*/
if (reSize > 2)
{
printf("Invalid Input1.");
}

/*If there's two elements left inside the array*/
else if (reSize == 2)
{
if (asciiCode[0] == 119)
{
printf("You have entered w");
storeUserOption = 1;
}

else if (asciiCode[0] == 116)
{
printf("You have entered t");
storeUserOption = 2;
}

else if (asciiCode[0] == 113)
{
printf("You have entered q");
storeUserOption = 0;
}

else{
printf("Invalid Input.");

}
}
}

}

最佳答案

您需要在 while 循环开始时重新启动变量。

int storeUserOption = 100; // needs to be something so it enters your while loop

While( ... ) {
char userInput[10] = {0};
int asciiCode[10];
int i;
int countZero = 0;



}

关于c - While 循环在第一个循环后不接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19986229/

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