Previous "Else" without "if" error

Asked

Viewed 1,352 times

-1

I am trying to correct and always erring in the same place, always with the if and the else or between them. The problem always occurs with other codes.

#include <stdio.h>
#include <stdlib.h>
int main ()
{
    float a, b, c, deslta, x1, x2, raiz;
    printf ("Apesente os valores: ");
    scanf ("%f", &a);
    scanf ("%f", &b);
    scanf ("%f", &c);
    if (a = 0);
    printf ("Nao e uma equação do 2 grau\n");
    else
    delta=b^2 – 4*a*c
    if delta < 0
    printf ("Equacao nao possui raizes reais\n");
    else
    raiz=sqrt(delta);
    x1=(-b+raiz)/(2*a);
    x2=(-b-raiz)/(2*a);
    printf ("“O valor de X1 e X2 são: ", x1, x2);
}

inserir a descrição da imagem aqui

5 answers

0

Several errors in sequence in your code. Let’s go one by one:

  1. if (a = 0)

    Here you are assigning zero to a, then checking if this value is true (zero is false). Then you overwrite the original value of a and the condition will always turn out to be false. To compare equality use the ==:

    if (a == 0)
    
  2. if (a == 0);

    The parole expects to receive a eating after her. You intended to perform printf ("Nao e uma equação do 2 grau\n");, but put a ; there. That alone is an empty command. If a for zero, do nothing. Here you caused the problem with the else, after all, as there are no keys { ... } on parole she can only receive one command, and you put two then. The second (the printf) is out of parole. When you arrive at else you’re already out of parole and he doesn’t know what to do.

  3. delta = b^2 - 4*a*c

    First of all, missing a period and comma at the end. And more importantly: ^ is not power. That operator does bitwise xor. For power you can use pow(b, 2). The function comes from #include <math.h>. Or much better: b*b.

  4. if delta < 0

    Missing parentheses! Must be if (delta < 0).

  5. raiz = sqrt(delta);

    Where does the function come from sqrt? It should include the #include <math.h> for this to work.

  6. printf ("O valor de X1 e X2 são: ", x1, x2);

    The printf will not contact the values for you. It also does not know detect the types of values you want to handle. Use so:

    printf ("O valor de X1 e X2 são: %g e %g", x1, x2);
    
  7. Return

    You have declared a function int main(). She returns a int, but you didn’t return one int! Your function is returning unspecified value, which may be problematic. Add at the end: return 0;. Traidicionally zero indicates that no error has occurred. You can use one for error. Or any other value of your choice.

  8. Arguments

    The function int main() takes any number of arguments of any kind. This is illegal for the function main. It must be: either take zero arguments, or take one int and a char**. Set it to zero arguments:

    int main(void)
    
  9. stray '\226' in program

    You are only allowed to use ASCII characters in C code. You used things like ç or ã. It is also likely that you typed some invisible (not printable) character in the middle of the code, confusing the compiler.

As you can see, there was a lot wrong with such a small block. Most of this is pure lack of attention with the syntax of C. I suggest a more detailed study of how it works.

0


#include <stdio.h>
#include <stdlib.h>
#include <math.h> /* para sqrt */
int main() {
    float a, b, c, delta, x1, x2, raiz;
    printf ("Informe os valores de a, b e c: ");
    scanf ("%f", &a);
    scanf ("%f", &b);
    scanf ("%f", &c);
    if (a == 0)
        printf ("Nao e uma equação do 2 grau\n");
    else
        delta = b*b - 4*a*c; /* No lugar de b*b poderia utilizar pow(b, 2) */
    if (delta < 0)
        printf ("Equacao nao possui raizes reais\n");
    else {
        raiz = sqrt(delta);
        x1 = (-b+raiz)/(2*a);
        x2 = (-b-raiz)/(2*a);
        printf ("Raizes X1: %f e X2: %f\n", x1, x2);
    }
    return 0;
}
  • the following code is displaying the [Error] Stray ' 226' in program which could be ?

  • 1

    In delta = bb - 4a*c; corrects the operator -. (- is different from -)

0

Please note the parentheses and the keys. No if you cannot put ; because it closes the block.

int main () { 
   float a, b, c, deslta, x1, x2, raiz; 
   printf ("Apesente os valores: "); 
   scanf ("%f", &a); 
   scanf ("%f", &b); 
   scanf ("%f", &c); 
   **if (a == 0){**
    printf ("Nao e uma equação do 2 grau\n"); 
   }else{ 
     delta=(b^2) – (4*a*c);
     if (delta < 0) {
       printf ("Equacao nao possui raizes reais\n"); 
     }else{ 
       raiz=sqrt(delta); 
       x1=(-b+raiz)/(2*a); 
       x2=(-b-raiz)/(2*a); 
       printf ("“O valor de X1 e X2 são: ", x1, x2);
      } 
   }
}

0

; Delimits the end of the line, it is not put in if and Else, as both have continuation.

= assigns value to a variable, in 10 the == operator should be used to denote equality.

Another error I identified was that delta is not declared (you misspelled on line 5).

Here’s an example of code structured in blocks:

if(a == 0){

}else{
  delta = b^2-4*a*c;
  if(delta < 0){

  }
}

-2

Two common mistakes in new programmers consist of: Lack of attention and not giving value to good practices.

Note that you have declared the variable as "delsta" and use it as "delta".

no if(a = 0) You made 2 mistakes. One of using a ";" where it shouldn’t and another error which consists of: when you want to test/compare 2 values you use "==" or "!=". In your case, an assignment was used "=" .

And another thing, although college professors insist on saying that if you only use one line (and not a block) in if/Else you don’t need "{}", it’s good practice to always use them.

Browser other questions tagged

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