-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 variablechar
, that is, it stores 1 character. To store a name it needs to be an array– Ricardo Pontual
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)
. Idemif
. As to the name statechar nome[3];
and use the format%s
in the scanf:scanf("%s", nome);
(without &).– anonimo
Typing error. Declare
char nome[50];
or the size it deems appropriate.– anonimo