0

我的代码有点问题。我在这里所做的基本上是使用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.");

                        }
                }
}

            }
4

1 回答 1

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;



}
于 2013-11-14T19:14:33.570 回答