First, my code asks your name and it shows a random number and you need to extract the square root of it. So if the code shows 81
, you need to type 9
. But, when the number is decimal (1.54896
for example), I tried to write when this occurs you just need to put the two digits after the point, but didn't work. And when you write the wrong answer, it shows many times the same word until you stop the code. How can I fix this? I am very new.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h> /* a função rand precisa dessa biblioteca*/
#include <math.h>
#include <time.h>
int main()
{
/* nome */
string nome = get_string("olá, qual é o seu nome? aaaaa ");
int a, c;
/* os valores q podem ser formados, até 10 valores */
printf("Olá, %s. Qual é a raiz quadrade de \n", nome);
srand(time(NULL));
for (a = 0; a < 1; a++)
/* o valor a ser gerado */
c = 10 rand() % 100 + 1;
printf("%d ", c);
/* o n é a resposta */
int n;
/* o b é o resultado, entao sua reposta(n) for igual ao resultado certo(b), vc ganha um parabéns */
float b;
scanf("%d", &n);
/* b é a raiz de a */
b = sqrt(c);
if (b == "%d")
{
("%0.2f", n);
}
else (n == b)
{
printf("parabéns, vc tem dois neurônios.\n");
}
else
{
while (n > b || n < b)
{
printf("tente de novo.\n");
n++;
}
}
return 0;
}
And when you put the wrong answer:
gcc loop2.c -o loop2teste20 -lm -lcs50
./loop2teste20
olá, qual é o seu nome? teste
Olá, teste. Qual é a raiz quadrada de
71 8,42
tente de novo.
tente de novo.
tente de novo.
...