Program not the first scanf and skip the second scanf of the script

Asked

Viewed 59 times

-1

I try to create an algorithm, but when I read the contents of the first scanf, it only prints the first letter of the name. The second scanf that should read a number is ignored and automatically prints the result of the first if

#include <stdio.h>

//Compiler version gcc  6.3.0

int main()
{
  int tentativas = 0;
  int nivel;
  int numeroDaSorte = rand() % 100;
  char nome;


  printf("Bem vindo ao jogo de adivinhacao\n");
  printf("\nQual o seu nome?\n");
  scanf("%c", &nome);
  printf("\n\nSeja Bem Vindo %c", nome);
  printf("\n Qual nivel de dificuldade vc deseja?");
  printf("\n1 - facil");
  printf("\n2 - medio");
  printf("\n3 - dificil");
  scanf("%d", &tentativas);

  if(tentativas = 1)
  {

     printf("\n Nivel facil, vc tem 15 tentativas ");
     nivel = 15;
     }
      else
      if(tentativas = 2) {
        printf("\n Nivel medio, vc tem 10 tentativas");
        nivel = 10;
        }
        else {
          printf("\n nivel dificil, vc tem 5 tentativas");
          nivel = 5;
        }

  }
  • that’s right, char nome is a variable char, that is, it stores 1 character. To store a name it needs to be an array

  • Here: if(tentativas = 1)you are not comparing the variable attempts with 1, you are assigning 1 to the variable attempts. To compare use the operator ==: if (tentativas == 1). Idem if. As to the name state char nome[3];and use the format %s in the scanf: scanf("%s", nome); (without &).

  • Typing error. Declare char nome[50]; or the size it deems appropriate.

1 answer

0

You need to study a little bit more about string, just like they said in the other answers. You have declared the name variable as char , a char is just a character, that is, only the first letter of the name you have entered. Import the string library. h, you will also need to change the name variable statement. Doing this , in the printf you will call it by %s (string) instead of %c (char) .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.