mercredi 30 juin 2021

Number guessing game always returns the same output

I'm making a simple number guessing game from 1 - 10 using an if...else statement, it worked the first time, but it seems it always outputs "You didn't guess it correctly." when I'm guessing or the other way around. Would be great for some help, so here are the solutions I made as of asking this question:

//This is the first solution I've come up with.
#include <stdio.h>
#include <stdbool.h>

int main() {
  int guessingNumber = '5';
  printf("Guess from 1-10: ");
  scanf("%d", &guessingNumber);

  if (guessingNumber == true) {
    printf("You guessed it!\n");
  }
  else {
    printf("You didn't guess it correctly.\n");
  }
  return 0;
}


//Here is the second one.
#include <stdio.h>

int main() {
  int guessingNumber = '5';
  int guess;
  printf("Guess from 1-10: ");
  scanf("%d", &guess);

  if (guessingNumber == guess) {
    printf("You guessed it!\n");
  }
  else {
    printf("You didn't guess it correctly.\n");
  }
  return 0;
}

I'm sure that the problem causing this is in this lines from the first solution:

printf("Guess from 1-10: ");
      scanf("%d", &guessingNumber);
    
      if (guessingNumber == true) {
        printf("You guessed it!\n");

and this from the second solution that I'm sure the problem (might also) came:

printf("Guess from 1-10: ");
  scanf("%d", &guess);

  if (guessingNumber == guess) {
    printf("You guessed it!\n");

Aucun commentaire:

Enregistrer un commentaire