Hello,

When I run my code below, it lets me pick a number and seems to go through my first IF statement fine. However, after I get through my first IF, I'm wanting it to then add to my tries so that when I go over 5 or more tries, I exit. It seems that it's ignoring my tries++ IF statment and only allows me to guess one time. Can someone take a look at this, I may have just banged my head too many times on this and just missing something right there... Thanks

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num = 0;
    int tries = 0;
    int randomNumber = rand()%100;

    printf(" Enter a number between 1 and 100\n");
             scanf("%d", &num);
             printf("\n");
     {

        if(num == randomNumber)
        {
           printf("You WIN!!!\n");
        }
        else
            if (num < randomNumber)
            {
           printf("Too LOW!!!\n");
            }
            else
           printf("Too HIGH!!!\n");
          }

        tries++;
        if (tries >=5)
        {
        printf("Too many tries, you lose!!!\n");
        }

      printf("\n\nEND PROGRAM!\n\n");
      system("PAUSE");
      return 0;
}

I guess you need a 'do' loop or a 'while' loop.

The opening brace on line 13 would seem to suggest that would be a good place to start.

You're...um...not using a loop at all. You can't expect code to repeat if it doesn't loop somehow.

The following code will help you create the situation you needed to perform.

for(tries=0; tries>=5; tries++)
    {

      char y = 'y';
      printf("Do yo wanna continue? (y/n) \n");
      scanf("%d", &y);
      
    }

if (tries >=5)
   {
    printf("Too many tries, you lose!!!\n");
   }

You might also want to include some error correction code in the loop, So that only Integers or characters (whatever) will be input, for more clarity.

>for(tries=0; tries>=5; tries++)
Perhaps you meant <=. As it is your loop won't ever execute because tries is initialized to 0, which I'm reasonably sure isn't greater than or equal to 5. ;) Also, <= gives you an off-by-one error if you were expecting the loop to execute five times. It'll actually execute six. You probably want a half-open range (the convention in C):

for ( tries = 0; tries < 5; tries++ )

><= gives you an off-by-one error
>Perhaps you meant <=. As it is your loop won't >ever execute because tries is initialized to 0
lol, yeah your right ^///^ Should have re-read my post before actually posting it. Sorry.

I got it working now, I was just thinking too hard thanks for the advice.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num = 0;
    int count = 0;
    int randomNumber = rand()%100;


    printf(" Enter a number between 1 and 100\n");  //ask for input
             scanf("%d", &num);   //take input and put into memory
             printf("\n");

    while(count<=3) //begin while loop
    {

       if(num==randomNumber)
       {
          printf("You WIN!!\n");
          printf("Number was: %d", randomNumber);
          break; //if condition true, break out of program
       }

       else if(num < randomNumber)
       {
         printf("Too LOW!!!\n");
       }
       else
       {
         printf("Too HIGH!!!\n");
       }
       printf(" Enter a number between 1 and 100\n");
             scanf("%d", &num);
             printf("\n");

       count++;
     };   //end while loop


       if(count >=4)  // test counter condition
       {
       printf("Too many tries, you lose!\n");
       }

      printf("\n\nEND PROGRAM!\n\n");
      system("PAUSE");
      return 0;      //end program
}

if(count >=4) You know that line really doesn't have any meaning according to your program. Since the value of count could never be greater than 3. The program will loop 4 times and then check if 3>=4, which is false, so that if statement will never execute. After 4 tries it'll just say the end of program.

This article has been dead for over six months. Start a new discussion instead.