POINTY
POINTY

Reputation: 13

If and if else statements working, but not else

I am making a number guessing game program and am having some trouble with my else statements. In the main block where the number is trying to be guessed, the if and if else statements work, but the else statement does nothing. I am trying to make it where a number outside of the range 0 < number < 100 trigger the else statement.

Furthermore, I am trying to make the game repeat itself if '1' is entered but no matter what is entered, the program crashes.

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

int main(void)
{
    /*Number guessing game: The number that needs to be guessed is 52*/
    int guess;
    int attempt = 6;
    int playAgain = 1;

    printf("Guess the integer that I am thinking of between 1 and 100\n");
    printf("I will tell you if you guess too high or too low\n");
    printf("You have 6 attempts\n");
    printf("What is your first guess?\n");
    if (playAgain == 1)
    {
        while (attempt > 0)
        {

            scanf_s("%d", &guess);

            if (guess > 0)
            {
                if (guess < 52)
                {
                    attempt--;
                    printf("Sorry! Too low! You have %d more tries:\n", attempt);
                }
            }
            else if (guess <100)
            {
                if (guess > 52)
                {
                    attempt--;
                    printf("Sorry! Too high! You have %d more tries:\n", attempt);
                }
            }
            else if (guess == 52)
            {
                printf("Correct! You win!\n");
                attempt = 0;
            }

            else
            {
                printf("Invalid input: Please enter an integer between 1 and 100:\n");
            }
        }

        printf("Enter '1' to play again or anything else to terminate\n");
        scanf_s("%d", playAgain);
        attempt = 6;
    }

    else
        {
            printf("Thanks for playing!\n");
        }
    return 0;

}

Upvotes: 0

Views: 188

Answers (5)

Jabberwocky
Jabberwocky

Reputation: 50778

Corrected and simplified version :

Your program crashes because you forgot the & sign inscanf("%d", &playAgain);.

The logic in your program is wrong, you intermix the test if the number is lower, equal or higher than the input with the test if the input is lower than 0 or higher than 100.

In this corrected version the "invalid input" problem is separated from the actuel "number guessing" problem.

Furthermore the number to be guessed (52) is nor longer hard coded but a variable numbertobeguessed is used instead. Later you should enhance the program so that a random number is generated.

int main(void)
{
  /*Number guessing game: The number that needs to be guessed is 52*/
  int numbertobeguessed = 52 ;
  int guess;
  int attempt = 6;
  int playAgain = 1;

  printf("Guess the integer that I am thinking of between 1 and 100\n");
  printf("I will tell you if you guess too high or too low\n");
  printf("You have 6 attempts\n");
  printf("What is your first guess?\n");

  if (playAgain == 1)
  {
    while (attempt > 0)
    {
      scanf_s("%d", &guess);

      if (guess < 0 || guess > 100)
      {
        printf("Invalid input: Please enter an integer between 1 and 100:\n");
      }
      else
      {
        if (guess < numbertobeguessed)
        {
          attempt--;
          printf("Sorry! Too low! You have %d more tries:\n", attempt);
        }
        else if (guess > numbertobeguessed)
        {
          attempt--;
          printf("Sorry! Too high! You have %d more tries:\n", attempt);
        }
        else
        {
          printf("Correct! You win!\n");
          attempt = 0;
        }
      }
    }

    printf("Enter '1' to play again or anything else to terminate\n");
    scanf_s("%d", &playAgain);
    attempt = 6;
  }
  else
  {
    printf("Thanks for playing!\n");
  }

  return 0;
}

Upvotes: 1

Shine Jacob
Shine Jacob

Reputation: 21

hope this helps

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

int main()
{
   int guess; //variable to hold the number from player
   int attempts = 6;
   char play = 'y';

   do
   {

    while(attempts)
   { 
     printf("\nEnter you guess: "); scanf("%d", &guess);

     attempts--;

     if(guess>0 && guess <100 )
     {
        if(guess>52)
          printf("\nThat's not it. Try something lower than that.");
        else if(guess<52)
          printf("\nThat's not the number. Try something higher than that.");
        else if(guess==52)
        { printf("\nYou got it!. You won the game."); 
          attempts = 0; //we're setting it to zero; we don't want the loop to run anymore
        }
     }
     else 
       printf("\nNumber enter is not in range!");
   }//end of while loop

     printf("\nDo you want to play again? (y/n): "); scanf("%c", &play);
   }while(play=='y' || play=='Y'); //run till player enter 'Y'
   return 0;
}

Upvotes: 0

Shomz
Shomz

Reputation: 37701

Your nesting is wrong. Put brackets for each if and else to make your code work (quickfix), and use proper indentation to make it readable to humans (if you wish).

Here's an example of how things can go wrong (pseudo code):

a = 4
if (a > 0)
    if (a < 3)
        a = 2
else
    a = 3

What do you expect is the end value of a?

Anyway, your:

if (guess > 0)
if (guess < 52)

should become this:

if (guess > 0 && guess < 52)

and your:

else if (guess <100) // this is where the problems start
if (guess > 52)

should become:

else if (guess < 100 && guess > 52)

and your code will work.

Upvotes: 0

LearningC
LearningC

Reputation: 3162

the else statement which gives message "Invalid input: Please enter an integer between 1 and 100:\n" is considered as the else part of inner most if-else-if statement. there fore that statement is never executed as the execution enters that if-else-if statement only if 0 < guess < 100. so use {} properly to make the proper combination of if-else statements.

Upvotes: 0

Yabada
Yabada

Reputation: 1758

When you use if else if without brackets, make sure it can't be ambigous.

When you do :

if (true)
   if (true)
   {
   }
else if (false)
{
}

How to know if the else if correspond to the first or the second if ? That's why everyone yell at you to put brackets.

if (true)
{
   if (true)
   {
   }
}
else if (false)
{
}

Upvotes: 2

Related Questions