Code executes a line that should be conditional

I'm doing some college work in C++, and when performing an if / else for a value of totalf equal to or different from 0, it's not obeyed. How do I make the output display only a certain printf of the correct condition ?

Follows the output:

Result

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;

    }

Code image and output

 2
Author: Maniero, 2019-06-07

5 answers

There was a ; where it shouldn't end else, so this clause of if did nothing and the next line executed unconditionally. This is the main error that causes

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

But it also has a problem that is more difficult to realize that is the type of data used. A type float has no accuracy and should not be used for comparison that requires this as it is doing. That is, it should never be used as a monetary value. Especially equality or difference can give false negatives because of lack of accuracy.

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

In this case the problem is not the if but the data type. And if is not a function, so it has no parameters, I think it's important to make those 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");
}

See working on Ideon. E no repl.it. also I put on GitHub for future reference .

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

 4
Author: Maniero, 2019-06-10 13:31:54

You don't need to put parameters in else, here's an example where it would make sense to use more than one if and how this 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);
}

Notice that else Is the default condition if none of the above conditions work.

 1
Author: Marcos Adriano, 2019-06-07 01:25:48

Your if e is correct, but 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 nonzero print XXXX, if not print yyyyy

Remove the bolean expression from else and your program should work normally.

 0
Author: Killdary Aguiar de Santana, 2019-06-07 01:39:11

There are 2 types of errors in programming , which are:

  • compile errors
  • execution errors

About compile errors

Occur when the program we write does not obey the rules of the language. These types of errors are usually easy to detect. The most common are:

  • forget a semicolon
  • forget to close a key
  • forget about close quotation marks in a printf or scanf
  • forget to close a comment
  • forget the variable declaration
  • misspell a word (ex: studio.h instead of stdio.h)

About execution errors

Are more delicate, imperceptible. The program compiles normally without errors, but when executed it does not do what we expect it to do. In computer slang, we call this type of errors by bugs .

Many programming beginners get frustrated when they can't detect the bugs. With experience, you'll see that they start to detect them quickly. The important thing is not to panic.

To detect a bug , you should try to isolate the error. This can be done by 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 program variables.

Below are some classic bugs that occur in. They are classic because there is no C programmer who has ever 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) no scanf
  • do not put the correct flags(%d,%c,%f,...) in printf or scanf
  • Use = instead of ==
  • Infinite Loop
  • semicolon in wrong place
  • Do Not group statements when you want a composite statement

Your code has some of these problems, so let's analyze 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 statements when you want a composite statement and do not enter 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 indent the printf and remove the expression (total == 0) do 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");
 0
Author: Comunidade, 2020-06-11 14:45:34

For ease of understanding, every time you read the word "else", translate to"otherwise". 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 don't have to put it inside else.

 -2
Author: Djair Dutra, 2019-06-07 01:30:28