Number guessing loop

What's wrong with this loop? \\this is a number guessing game.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>

    main()

    {
        int number;
        int guess;
        int tries;

        number = rand() % 101 + 1;

        printf("Let's play a game!\n");
        printf("I will think of a number 1-100. Try to guess it.\n");
        printf("Guess: ");
        scanf("%d",&guess);

      	for (tries = 0;tries<100;tries++);
        {
            if (guess == number)
            {
                printf("You guessed it!\n");
                scanf("%d",&guess);
                printf("And it only took you: ");
				scanf("%d",&tries);

            }
            else if (guess < number)
            {
                printf("Higher\n");
              	printf("Guess again: ");
        		scanf("%d",&guess);
            }


            else if (guess > number)
            {
                printf("Lower\n");
                printf("Guess again: ");
        		scanf("%d",&guess);
            }

            else
                printf("That's not even in range!");
            return 0;
Last edited on
Your braces are unbalanced.
You probably mean this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>

    main()

    {
        int number;
        int guess;
        int tries;

        number = rand() % 101 + 1;

        printf("Let's play a game!\n");
        printf("I will think of a number 1-100. Try to guess it.\n");
        printf("Guess: ");
        scanf("%d",&guess);

      	for (tries = 0;tries<100;tries++);
        {
            if (guess == number)
            {
                printf("You guessed it!\n");
                scanf("%d",&guess);
                printf("And it only took you: ");
				scanf("%d",&tries);

            }
            else if (guess < number)
            {
                printf("Higher\n");
              	printf("Guess again: ");
        		scanf("%d",&guess);
            }


            else if (guess > number)
            {
                printf("Lower\n");
                printf("Guess again: ");
        		scanf("%d",&guess);
            }

            else
                printf("That's not even in range!");
        }
    return 0;
    }


You probably want to put a break; so that you exit the loop if the guess is correct.

I also highly, highly recommend you initialize your variables. Just make it a habit to give every variable an initial value.
1
2
3
int number(0);
int guess(0);
int tries(0);
hello Jayhawk, Thnak you for your inputs, however, it seems that the for loop isnt working. It only allows me to have 2 tries then loop is exiting (w/o getting guess==number). Thanks in advance.
Also, why is it always the random number is 42.
closed account (48T7M4Gy)
JayHawk seems to have done all the hard work but it looks like you need to add the 'seed' line
1
2
/* initialize random seed: */
  srand (time(NULL));


See: http://www.cplusplus.com/reference/cstdlib/rand/
Last edited on
Hi kemort,

yeah, got that. Thank you. However, im having to trouble as the looping seems to be not working.

Any possible mistakes? Sorry Im still studying loops.
Im also trying to work out as to how to count the number of attempts by having this:
for (tries = 0;tries<100;tries++)

Here's my new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

    int main()

    {
        int number;
        int guess;
        int tries;
		srand (time(NULL));
        number = rand() % 100 + 1;

        printf("Let's play a game!\n");
        printf("I will think of a number 1-100. Try to guess it.\n");
     
      
	for (tries = 0;tries<=5;tries++);
	printf("Guess: ");
        scanf("%d",&guess);
      	            
		{
            if (guess == number)
            {
                printf("You guessed it!\n");
                printf("It only took you: %d tries");
		scanf("%d",&tries);

            }
            else if (guess < number)
            {
                printf("Higher\n");
              	printf("Guess again: ");
        	scanf("%d",&guess);
            }
            else if (guess > number)
            {
                printf("Lower\n");
                printf("Guess again: ");
        	scanf("%d",&guess);
            }
  	    }
  	    {
                if (tries=5);
  		printf("You have now reached the maximum tries. Thank you for playing!");
  	    }
		
    }


It only allows me to have two attempts then program breaks already. Also, everytime I put break; it always gives me an error that it should be within a loop.

Your help will be very much appreciated. Thank you.
Last edited on
Yes, break; must be within a loop. I suggested you use it in order to exit your loop early.

You only put part of your code, but this chunk should be looked at:
1
2
3
{if (tries=5);
printf("You have now reached the maximum tries. Thank you for playing!");
}


This is legal, but I don't think it's the logic you were going for. Should you be using = or ==?
You would also need to manually exit the loop yourself inside that chunk, since 5 is too low for the loop to automatically exit.
closed account (48T7M4Gy)
Lot's of small things to consider.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int number = 0; // <--- ALWAYS INITIALIZE VARIABLES
    int guess = 0;  //<---
    
    srand (time(NULL));
    number = rand() % 100 + 1;
    
    printf("Let's play a game!\n");
    printf("I will think of a number 1-100. Try to guess it.\n");
    
    for (int tries = 0; tries < 5; tries++) //<---
    {
        printf("Guess: "); // <-- ONCE ONLY
        scanf("%d", &guess);// <-- ONCE ONLY
        
        if (guess == number)
        {
            printf("You guessed it!\n");
            printf("It only took you: %d tries", tries); // <---
            break; // <--
        }
        else if (guess < number)
        {
            printf("Higher\n");
        }
        else
        {
            printf("Lower\n");
        }
    }
    printf("You have now reached the maximum tries. Thank you for playing!");
    
    return 0;
}
Topic archived. No new replies allowed.