Question

The game works fine my first time through, although; the second time it only gives you two lives... I have tried to change the number of lives but still can't figure out what I'm doing wrong.

// C_program_random_number_game

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

int main()
{

   srand(time(NULL));
   int num1,x = 0;
   char game, cont, replay;

   printf("Would you like to play a game? : ");
   scanf("%c",&game);

  if (game == 'y' || game == 'Y')
  {
    printf("\nThe rules are simple. You have have 5 tries to guess the computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");
    do
    {
        int r = rand()%5 +1;
        printf("\n\nEnter a number between 1 and 5 : ");
        scanf("\n%d",&num1);
        x++;
        if(num1 > 0 && num1 < 5)  
        {

            do
            {
             if(num1 < r)
                {
                    printf("\nClose! try a little higher... : ");
                    x++; 
                }
              else if (num1 > r)
                {
                    printf("\nClose! try a little lower...: ");
                    x++; 
                }
                scanf("%d",&num1);

            }while(num1!=r && x <3); 

            if(num1 == r) 
            {
                printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
            }
            else if(num1 != r)
            {
                printf("\nBetter luck next time!");
            }
            printf("\n\nWould you like to play again? (y or n) : ");
            scanf("\n%c",&replay);
        }
            else
        {
            printf("Sorry! Try again : ");
            scanf("%d",&num1);
        }

    }while(replay == 'y'|| replay == 'Y');

}
else if (game == 'n' || game == 'N')
{
    printf("Okay, maybe next time! ");
}
else
{
    printf("Sorry, invalid data! ");
}
return 0;

}

Was it helpful?

Solution

There are all kinds of issues with you code (most of them are minor in terms of programming). Most of the errors are typos in what you want done via this question and what you printf().

As is, this code will random between 1-25, accept an input of any valid int, see if you matched it, and only give you 5 tries. (I didn't add error checking to enforce that the input is between 1-25. That should probably be added.)

I commented my code below with all my changes and went by that you had in the printf()s.

Note: See my comments above for explanations of my changes since I already pointed them out. I also formatted it so its a little more easy to read.

Note2: I did this quickly using an online compiler. If you find anything wrong with this or not working as you'd like, just comment below and I'll address it.

// C_program_random_number_game

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

int main()
{

    srand(time(NULL));
    int num1,x = 0;
    char game, cont, replay;

    printf("Would you like to play a game? : ");
    scanf("%c",&game);

    if (game == 'y' || game == 'Y')
    {
        printf("\nThe rules are simple. You have have 5 tries to guess the                 computers number. \n \n If you succeed you win the game, if you dont you lose the game. Good luck!");

        do
        {
            int r = rand()%25 +1;

            printf("\n\nEnter a number between 1 and 25 : ");
            scanf("%d",&num1);

            do
            {
                printf("r = %d\n", r);

                if(num1 < r)
                {
                    printf("\nClose! try a little higher... : ");
                    x++; //Increment x if wrong guess
                }
                else if (num1 > r)
                {
                    printf("\nClose! try a little lower...: ");
                    x++; //Increment x if wrong guess
                }

                scanf("%d",&num1);
            }while(num1!=r && x < 5); //If x is 5 or more, they ran out of guesses (also, you want an && not an ||)

           if(num1 == r) //Only print "winner" if they won!
           {
               printf("\nWinner! >> you entered %d and the computer generated %d! \n",num1, r);
           }

            printf("\nWould you like to play again? (y or n) : ");
            scanf("\n%c",&replay);
        }while(replay == 'y'|| replay == 'Y');
    }

    printf("Thanks for playing! ");

    if (game == 'n' || game == 'N')
    {
        printf("Okay, maybe next time! ");
    }
    return 0;
}

OTHER TIPS

There are a combination of two problems. The first is that you're not breaking out of the "for" loop when the number matches. Therefore the match is only checked on every third guess.

The second problem is in this logic check:

}while(num1!=r || x <= 3);

We see that this turns into "true" if the for loop is broken out of early.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top