I wrote this code in Dev C++, it compiled but does not perform the operation

Asked

Viewed 149 times

-2

When I run and type a number it does not multiply, it only displays the message " Price:" and not the multiplication result.

#include<stdio.h>
main()

{

int quantidade;
double valor;
scanf("%d", &quantidade);

        if(quantidade<12)
        {
          printf ("Preço:");
          valor=quantidade*1.3;

        }
        else
        {
         printf ("Preço:");
         valor=quantidade*1.0;

        }
  • He’s doing exactly what you told him to do. He even does the multiplication. But for some reason it seems that it is not what you want, so you would need to write in the question what should have happened in your conception.

  • It does not display the result on execution, only appears "Price:".

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

4

The biggest problem is that you are not having the result printed. Everything you want the code to do you have to say. And you don’t have to say anything that’s unnecessary. This code could be simplified since its purpose seems to be to read only one number and depending on it generate another number based on it multiplied by a predetermined factor. Then you only need to have a variable to store the requested data to type, ask to type and have printed the calculation, which can be done in a simple conditional expression. Reinforcement, if you do not print the result it will not be printed, nothing happens like magic in the code. And compiling is an unnecessary concern, what matters is the code being right, having compiled is not measure of something certain.

#include <stdio.h>

int main(void) {
    int quantidade;
    scanf("%d", &quantidade);
    printf("Preço: %lf", quantidade * (quantidade < 12 ? 1.3 : 1.0));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The question talks in C++, but this code is C and not C++, even if it is C++, it is good to keep in mind that it is not C++. And I wouldn’t use this IDE, it’s quite criticized.

Browser other questions tagged

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