Code executes a line that should be conditional

Asked

Viewed 192 times

2

I am doing a job for college at C++ and, when performing an if/Else for a value of totalf equal to or different from 0, it is not obeyed. How do I exit display only a certain printf of correct condition?

Follow the exit:

Resultado

Follows the code:

 #include <stdio.h>

 int main() {

       int filial;
       float v1, v2, v3, totalf;

       printf("\ndigite o numero da filial: ");
       scanf("%d", &filial);

       printf("Digite o valor do 1 mes de vendas: ");
       scanf("%f", &v1);

       printf("Digite o valor do 2 mes de vendas: ");
       scanf("%f", &v2);

       printf("Digite o valor do 3 mes de vendas: ");
       scanf("%f", &v3);

       totalf = v1 + v2 + v3;

           if(totalf != 0)
       printf("\no valor total desta filial sera de: %f", totalf);
           else(total == 0);
       printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");

       return 0;

    }

Image of code and output

  • I suggest you read the community guidelines to make a good question.

  • 4

    I will recommend abandoning Dev-C++. This IDE has not been updated for many years and contains numerous problems. There are better alternatives. Right now, I’m recommending Visual Studio Community Edition for having a good support and numerous tutorials for using the IDE. It also works on Linux.

  • @Jose, I suggest Douglas use the Code::Blocks Ides.

  • I believe that the question should not be edited to correct the code posted by the author. The way it turned out some answers were totally meaningless. If you want to correct the code do in your reply.

  • @THIAGODEBONIS I recommend that he use an IDE like Atom or Sublime Text. For the build use a compiler like GCC or Mingw (if it’s Windows).

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

Show 2 more comments

5 answers

4

There was a ; where I shouldn’t be finishing the else, then this clause of if did nothing and the next line performed unconditionally. This is the main error that does what occurred

In addition there is a condition in the else, this is not necessary in this case and in the form realized nor can it be so, or leaves without condition or makes a else if (when fit, not in this case). So my recommendation is always to use if or else from a row, always put on the same parole line as I did in the code below. Or else use keys always. It would not solve this problem and nor would it be avoided, but it gets an easier look.

But there’s also a problem that’s harder to understand that it’s the type of data used. A type float is not accurate and should not be used for comparison that requires it as it is doing. That is, it should never be used as monetary value. Especially equality or difference can give false negatives because of inaccuracy.

I minimized the problem by checking if it is bigger, but it can still give some error situation, unlikely, but it can. The right thing is not to use this given tip ode. See more in What is the correct way to use the float, double and decimal types?.

In this case the problem is not the if rather the type of data. And the if is not a function, so it has no parameters, I think it is important to make these things clear.

 #include <stdio.h>

 int main() {
    int filial;
    float v1, v2, v3;
    printf("\ndigite o numero da filial: ");
    scanf("%d", &filial);
    printf("Digite o valor do 1 mes de vendas: ");
    scanf("%f", &v1);
    printf("Digite o valor do 2 mes de vendas: ");
    scanf("%f", &v2);
    printf("Digite o valor do 3 mes de vendas: ");
    scanf("%f", &v3);
    float totalf = v1 + v2 + v3;
    if (totalf > 0) printf("\no valor total desta filial sera de: %f", totalf);
    else printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");
}

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

As already said should avoid Devc++, although your problem has nothing to do with it, and should program C++ in C++, is using C code in C++. Choose which of the two will program, are different languages, although C++ understands C code.

  • Oh my master! What do you think of my answer to the same, did I go into many details? I save myself on your kind of answers to create mine, because I’m new in the community.

  • 1

    I don’t own the truth, but I see some problems in your answer. Very bold, it loses its meaning when there are so many, but nothing serious. I like detailed answers, but I think you have talked a lot about something that is close to zero importance to the question, not even to understand the context of the error, although it is good content. Then begins what I found a little worse and has more importance: You do not need to initialize the variables in this case because all variables will be filled with entries or calculated in the first moment, if you look at my code become even more evident. It’s not wrong to do it, but...

  • 1

    ...does not need and is a recommendation that borders on misunderstanding. And does not solve the problem, so at this point the answer is not good.

  • @Maniero had another mistake they took out of the question when transcribing the code: else(total == 0). I put him back there because he’s part of the problem. Need an edit on your reply citing this to close with Golden Key since you probably saw the version without the error.

  • @Jose thanks, I just edited talking about it.

  • @Maniero according to your feedback updated my response as well.

  • @THIAGODEBONIS of course improved, but not much because in essence only messed with the formatting that was something absolutely secondary.

  • @Maniero you are very demanding :D. I gave a more complete answer, because he is a beginner and I tried to explain in more detail the types of main errors and which are, at the end I edited the answer also related to else who had changed pointing out their mistakes.

  • @THIAGODEBONIS actually you saw that your edition ended up generating confusion and hid the real problem?

  • thank you very much helped, really I’m having some problems with this DEV IDE, I will use others and the issue of float, I did not know it could be used this way, thanks for the tip I will continue to strive and learn from vcs, really I get a little confused and am beginner in this area and will seek to learn more from you. @Maniero

Show 5 more comments

1

You don’t need to put parameters in Else, see an example where it would make sense to use more than one if and how it would be done:

if(a>5){
   printf("a é maior que 5);
} else if (a < 5){
   printf("a é menor a 5);
} else {
   printf("a é igual a 5);
}

Note that Else is the default condition if none of the above conditions work.

0

Your if and is correct, but the else it does not receive any parameter or boolean expression, as it is a consequence of if, where the literal translation would be:

if totalf is non-zero print XXXX, if not print YYYYY

Remove the boleana expression of else and your program should work normally.

0

There are 2 types of programming errors, which are:

  • compilation errors
  • errors of execution

About the Compilation Errors

They occur when the program we write does not obey the language rules. This type of error is usually easy to detect. The most frequent ones are:

  • Forget a semicolon
  • Forget to close a key
  • Forget to close the quotation marks on a printf or scanf
  • Forget to close a comment
  • Forget declaration of variables
  • Misspelling a word (e.g.: studio.h instead of stdio.h)

About the Errors of Execution

They are more delicate, imperceptible. The program compiles normally without errors, but when running it does not do what we expect it to do. In computer jargon, we call this bug type by bugs.

Many programming beginners are frustrated when they fail to detect the bugs. With experience, you will see that you start to detect them quickly. The important thing is not to panic.

To detect a bug, should try to isolate the error. This can be done using printf in the middle of the code to see what the program is doing step by step. Alternatively, there are special programs called debuggers that allow you to run the program step by step and see the contents of the variables of the program.

Below are some of the bugs classics occurring in C. They are classic because there is no programmer for C who have never made them. I am sure that sooner or later all of you will also make (or have already made) these mistakes, which are:

  • Forget to initialize variables
  • Forget the & (commercial) in scanf
  • Do not put the flags correct (%d,%c,%f,...) in the printf or scanf
  • Utilise = instead of ==
  • Infinite cycle
  • Semicolon in the wrong place
  • Do not group instructions when a composite instruction is desired

Your code has some of these problems, so let’s review the same now to make the corrections...

First Problem (Forgot to initialize variables):

int filial;
float v1, v2, v3, totalf;

Correction:

int filial = 0;
float v1 = 0.0, v2 = 0.0, v3 = 0.0, totalf = 0.0;

Second Problem (Do not group instructions when you want a compound instruction and do not insert logical conditions in the else):

  if(totalf != 0)
printf("\no valor total desta filial sera de: %f", totalf);
  else(total == 0)
printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");

Correction (Correctly identify the printf and remove the expression (total == 0) of else:

if(totalf != 0)
   printf("\no valor total desta filial sera de: %f", totalf);
else
   printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");

Or (Close Code Block with Keys):

 if(totalf != 0) {
    printf("\no valor total desta filial sera de: %f", totalf);
 } else {
    printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");
 }

Or (Use Ternary Conditional Operator):

(totalf != 0) ? printf("\no valor total desta filial sera de: %f", totalf) : printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");

-2

To facilitate understanding, each time you read the word "Else", translate to "else". In a simple if, as in this case, the condition != 0 has already been tested first, so it is understood that the other logical alternative is the sentence =0, so you do not need to put it inside Else.

Browser other questions tagged

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