2

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.
...
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Sanogo
  • 23
  • 4
  • 2
    There are many problems with this code. You should start by at least getting it to compile cleanly, which it doesn't. – Tom Karzes Jul 07 '22 at 02:22
  • The reason for your verbiage ("tente de novo") constantly printing in an endless loop is that your program reached that block of code because variable "b" is not equal to "n". That makes the test for the "while" loop always true so the "printf" command is executed and never breaks out of the "while" loop. So you probably need to start with at least removing the "while" loop at that point. – NoDakker Jul 07 '22 at 03:07
  • What is the calculation for ```c``` supposed to be? ```c=10*rand()%100+1``` ? – PurpleHacker Jul 07 '22 at 04:56
  • What is the point of the for loop increasing variable ```a```? You never use that value – PurpleHacker Jul 07 '22 at 04:57
  • 1
    Given that `b` is a `float`, the line `if (b == "%d")` is very peculiar (and won't compile cleanly, if it compiles at all); the body of the `if` is `("%0.2f", n);` which is even weirder (it's a no-op). There is so much wrong with the code shown that you'd do best to delete that file and start from scratch. – Jonathan Leffler Jul 07 '22 at 05:23
  • Also, you are incrementing integer n by 1 each iteration of the while loop, but if b is a decimal number, then you will in most cases pass it, which is part of the reason you have an endless loop – PurpleHacker Jul 07 '22 at 06:31

1 Answers1

1

There are multiple problems in your code:

  • for (a = 0; a < 1; a++) just iterates once, not very useful and definitely not needed in a minimal example.

  • c = 10 rand() % 100 + 1; is a syntax error. Use c = rand() % 100 + 1;

  • scanf("%d", &n) may fail if the input does not start with a number. You should test the return value and complain if input is invalid.

  • if (b == "%d") is meaningless: b is a float, you cannot compare that to a string. It is unclear what you are trying to achieve with this test.

  • ("%0.2f", n) does not do anything. Did you mean printf("%0.2f", n)? This would be incorrect too as n is an int and %0.2f expects a float or a double.

  • printf("parabéns, vc tem dois neurônios.\n"); Let's hope the user has a sense of humor :)

  • else (n == b) is a syntax error, you probably mean else if (n == b)

  • while (n > b || n < b) { n++; } runs forever if b is not an integer, until n reaches INT_MAX and you get undefined behavior. This explains why you get repeated output of tente de novo.

Here is a modified version:

#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    /* nome */
    string nome = get_string("Olá, qual é o seu nome? ");

    srand(time(NULL));

    /* o valor a ser gerado */
    int c = rand() % 100 + 1;

    /* os valores q podem ser formados, até 10 valores */
    printf("Olá, %s. Qual é a raiz quadrade de %d\n", nome, c);

    /* o n é a resposta */
    float n;

    /* o b é o resultado, entao sua reposta(n) for igual ao resultado certo(b), vc ganha um parabéns */

    /* b é a raiz de c */
    float b = sqrtf(c);

    for (int i = 0; i < 10; i++) {
        if (scanf("%f", &n) != 1) {
            printf("not a number\n");
            return 1;
        }
        if (roundf(n * 100) == roundf(b * 100)) {
            /* if the number input is closer than 0.01 */
            printf("parabéns, vc tem dois neurônios.\n");
            break;
        } else {
            printf("tente de novo.\n");
        }
    }
    return 0;
}

Output:

Olá, qual é o seu nome? aaaaa 
Olá, aaaaa. Qual é a raiz quadrade de 90
9.48
tente de novo.
9.49
parabéns, vc tem dois neurônios.
chqrlie
  • 131,814
  • 10
  • 121
  • 189