0

So in my code you have to guess a number if you under or overestimate. How do I get the while loop to loop back to the input so you can input another number?

#include <stdio.h>

int main(int argc, const char * argv[])
{
    int number = 45;

    printf("Please insert a number");
    scanf("%d", &number);


    if (number == 45){
        printf("good job! You got the number right.");
        return 0;
    }
    else if (number < 45){
        while (number < 45 || number > 45){
            printf("Your number is too low");
            return 1;
        }
    }
    else if (number > 45){
        printf("Your number is too high!");
        return 1;
    }
}
3
  • while (number != 45)... Commented Oct 31, 2013 at 22:36
  • 2
    Review the basics ... Commented Oct 31, 2013 at 22:37
  • 1
    Well, you can't get this loop to loop anywhere else. The body of the loop is what you included into the body of the loop. If something you want to be inside the loop happens to be outside, then.. well... you need a bigger loop, I guess. Commented Oct 31, 2013 at 22:37

2 Answers 2

1

Just put a while (1) around your code and remove the one wrongly placed :

#include <stdio.h>

int main(int argc, const char * argv[])
{
    while (1) {
        int number = 45;

        printf("Please insert a number");
        scanf("%d", &number);


        if (number == 45){
            printf("good job! You got the number right.");
            return 0;
        }
        else if (number < 45){
            printf("Your number is too low");
        }
        else{
            printf("Your number is too high!");
        }
    }
}

return is closing the program. So you don't want to return when the user hasn't guessed your number.
while (1) is the loop which will only exit when return 0 is launched (when the player has won).

Also, your last else if is not needed. Replace it by a else, because it is the only remaining case.

Sign up to request clarification or add additional context in comments.

4 Comments

With this code if user enters some word, let's say "hi", scanf will not modify number and the program will terminate with good job! You got the number right
That's just not related to the question. He's obviously learning the basics, so talking about the error cases isn't really a good way to go.
I can't think of any better thing to do at his place than reading the documentation of functions he's using (to also better understand the code) and handling the associated return codes properly.
You're welcome. But know that I also agree that this is basic stuff you would learn by following some tutorials / reading manuals.
1

Wrap your code with a loop:

int number = 45;

while (1) {
    printf("Please insert a number");
    if (scanf("%d", &number) != 1) {
        printf("not valid number, try again");
        continue;
    }
    if (number == 45){
        printf("good job! You got the number right.");
        return 0;
    }
    if (number < 45){
        printf("Your number is too low");
        continue;
    }
    if (number > 45){
        printf("Your number is too high!");
        continue;
    }
}

Note that you should also check the return value of scanf to find out whether the number has been successfully read.

1 Comment

This worked! Thank you! I feel like an idiot from all the other comments around here. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.