我的代码有点问题。我在这里做的基本上是使用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.");
}
}
}
}
答案 0 :(得分:1)
您需要在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;
}