1

I'm attempting to do a do-while loop where the user inputs a value, either 1 or 0, and if it is not 1 or 0 then the loop will ask them to do it again until they enter 1 or 0. However, in runtime, when i enter 1 or 0, it will keep printing the stuff inside the if statement

you have given an incorrect response, it must either be 1 or 0

even though i have entered 1/0, staying in this loop forever. Does it store the enter key? What am i missing and how do i fix this?

int validateresultmanual(char* word, char* suggestion)
{
    int choice;
    int result;

    if(suggestion == NULL)
    {
        result = FALSE;
    }
    else
    {
        do
        {
            printf("Enter 0 for yes or 1 for no: Do you want to change %s to %s?\n", word, suggestion);
            scanf("%c", &choice);
            if(choice != 0 || choice != 1)
            {
                printf("You have given an incorrect response, it must either be 1 or 0\n");
            }
            else if(choice == 0)
            {
                printf ("\n yaaaaaaaaaaaaa \n");
                result = TRUE;
            }
            else if (choice == 1)
            {
                result = FALSE;
            }
     } while (choice != 0 || choice !=1 );
}
return result;

}

3
  • 1
    Hint: think of a number choice for which both choice != 0 and choice != 1 would evaluate to false; that's the number you need to enter in order to end the loop. Commented Oct 25, 2017 at 15:35
  • Yes, it will read in the enter key. Commented Oct 25, 2017 at 15:39
  • Mega-dupe. Many, many similar Q's:( Commented Oct 25, 2017 at 15:51

2 Answers 2

4

Your situation check is incorrect:

change this

if(choice != 0 || choice != 1)

to

if(choice != 0 && choice != 1)
Sign up to request clarification or add additional context in comments.

Comments

0

replace the or with an and, because your while is checking for a condition and continuing while that condition is true.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.