problem with scanf

Asked

Viewed 63 times

0

I’m starting in C... and it always happens that the compiler ignores the scanf... but I do not understand why. This program is giving this error and I am not able to solve, if you can help me, thank you!

#include <stdio.h>
#include <string.h>

/* O programa deve perguntar qual a figura geométrica, e então pedir para o
usuário digitar os tamanhos dos lados e calcular a area. 
*/

float quadrado(){
    float lado;

    printf("\nDigite o valor de cada lado: ");
    scanf("%f", &lado);
    
    return  printf("\nA area do quadrado é de %f", (lado*2 ));
};

float retangulo(){
    float base,altura;
    
    printf("\nDigite o valor da altura: ");
    scanf("%f", &altura);
    
    printf("\nDigite o valor da base: ");
    scanf("%f", &base);
    
    return  printf("A area do retangulo é de %f", (base*altura));
};

int main() {
    char figura;
    printf("Qual a figura geometrica? (quadrado ou retangulo)");
    scanf("%c", &figura);
    
    if(strcmp(&figura,"quadrado")){
        quadrado();
    } else if (strcmp(&figura,"retangulo")){
        retangulo();
    } else{
        printf("Figura invalida");
    }

}
  • You are setting your function quadrado as returning a float but in charge return this function specifies what will be returned by the function printf which, according to the manual. will be the number of bytes transmitted. Maybe you wanted to return lado*2. Iofc retangulo. Note that in the calling function (main) you ignore what is returned by these functions. If it’s just to print what you want in your functions void and print and don’t give a return.

  • The area of the square should not be lado * lado?

2 answers

0


In its main function, figura is declared as a char.

"Char" is short for Character, which is "Caractér" in English. It is a data format to represent only one character. If you want to save an entire string, you have to reserve space for several characters. The easiest way to do this is in the variable declaration to declare an array of them:

char figura[30];

In this case, we set space in the computer’s memory for an array of 30 characters. This means that your string can have up to 29 of them. This is because you need to save a character that delimits the "end of the string". So when the computer arrives at this character, it knows that the string has ended up in memory. Functions like strcmp that you used need that terminator character. If you need clarification on this subject recommend doing a search here on the site or ask another question.

Another adaptation you need to make then is that the flagof scanf should no longer be %c. And yes %s to denote a string:

scanf("%s", figura);

Finally, how figura has become an array, meaning it no longer needs the signal & before. The very word figura already denotes a pointer. This should be changed in strcmp and in the scanf.

And with these changes your program runs the proper way (and without any errors or Warning):

Qual a figura geometrica? (quadrado ou retangulo)quadrado                                                             
                                                                                                                      
Digite o valor da altura: 10                                                                                          
                                                                                                                      
Digite o valor da base: 10                                                                                            
A area do retangulo é de 100.000000 
  • Thank you! You helped me out..

  • @You’re welcome! Do not forget to mark the answer as accepted, if she has answered and help any searches on the site.

-1

With the fixes explained in the comment and fixing your string definition (array of characters and not a single character, see if this fits:

#include <stdio.h>
#include <string.h>

/* O programa deve perguntar qual a figura geométrica, e então pedir para o
usuário digitar os tamanhos dos lados e calcular a area. 
*/

void quadrado(){
    float lado;

    printf("\nDigite o valor de cada lado: ");
    scanf("%f", &lado);
    
    printf("\nA area do quadrado é de %f", (lado*2 ));
};

void retangulo(){
    float base,altura;
    
    printf("\nDigite o valor da altura: ");
    scanf("%f", &altura);
    
    printf("\nDigite o valor da base: ");
    scanf("%f", &base);
    
    printf("A area do retangulo é de %f", (base*altura));
};

int main() {
    char figura[20];
    printf("Qual a figura geometrica? (quadrado ou retangulo)");
    scanf("%s", figura);
    
    if(strcmp(figura,"quadrado")){
        quadrado();
    } else if (strcmp(figura,"retangulo")){
        retangulo();
    } else{
        printf("Figura invalida");
    }
    return 0;
}

Browser other questions tagged

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