Home > OS >  How I can reduce the decimal numbers after the point and fix a bug that show a lot of words?
How I can reduce the decimal numbers after the point and fix a bug that show a lot of words?

Time:07-08

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.
...

CodePudding user response:

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.
  • Related