Loop repeating itself?

拥有回忆 提交于 2020-01-06 04:28:26

问题


I am currently trying to teach myself C since I believe that will be a good segue into C++ and C# (as well as getting a headstart prior to the start of classes). So I have decided to write this loop here:

#include <stdio.h>

int main()
{
    bool continueLoop = true;
    char response;
    printf("ARE YOU READY TO RUMBLE?!?!\n Y/N\n");
    response = getchar();
    int counter = 0;

        do
        {
            counter++;
            if (response == 'Y')
            {
                printf("AWESOME!");
                continueLoop == false;
                return 0;
            }
            else if (response == 'N')
            {
                printf("YOU FAIL!");
                continueLoop == false;
                return 0;
            }
           if (continueLoop == true)
            {
                printf("I do not understand your input!\n");
                printf("Please reinput! Y/N\n");
                response = getchar();
            }
            if(counter == 5)
            {
                printf("Exiting!");
                continueLoop == false;
                goto exit;
            }
        }while (continueLoop == true);

    exit:
        return 0;
}

My question(s) is(are) as follows: Why is it when if I input for example 'M' as my answer, it will loop itself twice; however if it is given the proper condition then terminates correctly.

Also, instead of getchar() should I instead convert response into a single length of array and then try to compare it somehow, or perhaps it should be done via the printf statement as so printf("ARE YOU READY TO RUMBLE?!? \n %s", response);

If it helps, I am using C-Lion as my IDE as I refuse to write any code in vi, emacs, or notepad.

EDITED CODE

int main()
{


    char response;
    printf("ARE YOU READY TO RUMBLE?!?!\n Y/N\n");
    scanf(" %c", &response);

    int counter = 0;


    while (counter < 5)
    {
        counter++;
        if (response == 'Y')
        {
            printf("AWESOME!");

            return 0;
        }
        else if (response == 'N')
        {
            printf("YOU FAIL!");

            return 0;
        }
        else
        {
            printf("I do not understand your input!\n");
            printf("Please reinput! Y/N\n");
            response = getchar();
        }

    }


    return 0;


}

回答1:


When you input M, you're not really inputting M only; you are inputting M followed by a newline. So getchar(3) will return M when you first call it, and then it will return \n on the second call. Hence the loop executes twice.

You can capture input and ignore whitespaces (newlines, tabs, spaces, etc) with scanf(" %c", &response). Note the leading whitespace in the format string; it is required to force scanf(3) to skip blanks.

Also these statements inside the first two if's are useless:

continueLoop == false;

You compare continueLoop to false and then throw the result away (if you're not compiling with -Wall, you should because this will very likely give you a warning).

You probably want assignment instead of comparison:

continueLoop = false;



回答2:


better to use scanf("%c",&ch); instead of getchar() because it will return '\n' second time when you call . else other code looks fine .

As, one of my friend pointed out the same behaviour for scanf is same. then you can use getc

c = getc(stdin);



回答3:


You have several mistakes in your code.

first if you want to repeat something N amount of times, use a for loop, instead of a while. Second if you want to interrupt the loop early, try using a break, instead of a boolean. Finally its redundant to change the boolean to false and then return 0;, since you're exiting the function.

int main() 
{
    char response;
    int i=0;
    for (i=0; i<5; i++)
    {
        printf("ARE YOU READY TO RUMBLE?!?!\n Y/N\n");
        response = getchar();
        if (response == 'Y' || response == 'y')
        {
            printf("AWESOME!");
            break;
        }
        else if (response == 'N' || response == 'n') 
        {
            printf("YOU FAIL!");
            break;
        }
        else
        {
            printf("I do not understand your input!\n");
        }
    }
    return 0;
}


来源:https://stackoverflow.com/questions/31521746/loop-repeating-itself

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!