Program "jumping" Variables

Asked

Viewed 631 times

0

The program is skipping the variables without even putting the number I want to use on it. All I can do is put the first one in and then it jumps and it goes to zero.
I don’t know if it’s program error Dev-C++ or the code.


Erro


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
    float basemaior, basemenor, altura, resultado;
    printf ("entre com a base maior do trapezio: ");
    scanf ("f%", &basemaior);
    printf ("entre com a base menor do trapezio: ");
    scanf ("f%", &basemenor);
    printf ("entre com a altura do trapezio: ");
    resultado=(basemaior+basemenor)*altura / 2;
    printf ("\no calculo da area de um trapezio e: f%", resultado);
    system ("PAUSE");
}
  • 1

    I suggest you turn on your compiler warnings.

2 answers

3


Your code has 3 errors:

  1. Instead of void main(), use int main(). Some compilers may not accept the first form. So it is recommended to use int main(), being the int returns from your program, indicating that the program has been run successfully, for example.
  2. Where is f%, the correct is %f.
  3. We’re missing a scanf() to the height.

Stay like this:

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

int main()
{
    float basemaior, basemenor, altura, resultado;
    printf ("entre com a base maior do trapezio: ");
    scanf ("%f", &basemaior);
    printf ("entre com a base menor do trapezio: ");
    scanf ("%f", &basemenor);
    printf ("entre com a altura do trapezio: ");
    scanf ("%f", &altura);
    resultado=(basemaior+basemenor)*altura / 2;
    printf ("\no calculo da area de um trapezio e: %f\n", resultado);
    system ("pause");

    return 0; // por causo do int main
}

2

The problem is that you have changed the position format specifier.

Instead of f%, trade them in for %f.

Also, for the calculation to be done correctly, add a scanf to the height:

scanf ("%f", &altura);

Browser other questions tagged

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