"else" error without previous if

-1

I'm trying to fix and always err in the same place, always with if and 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);
}

    
asked by anonymous 03.04.2014 / 22:09

5 answers

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;
}
    
03.04.2014 / 22:16
0

; It delimits the end of line, does not put in if and else, since both have continuation.

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

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

Here is an example of block-structured code:

if(a == 0){

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

  }
}
    
03.04.2014 / 22:21
0

Please note the parentheses and the braces. No if you can not 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);
      } 
   }
}
    
03.04.2014 / 22:17
0

Multiple errors in a string in your code. Let's go one by one:

  • if (a = 0)

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

    if (a == 0)
    
  • if (a == 0);

    The conditional expects to receive a meal after it. You intended to run printf ("Nao e uma equação do 2 grau\n"); , but put ; there. That alone is an empty command. If a is zero, do nothing. Here you caused the problem with else , after all, since there are no keys { ... } in the conditional it can only receive a command, and you have two then. The second ( printf ) is out of conditional. When it arrives in else you have already left the parole and it does not know what to do.

  • delta = b^2 - 4*a*c

    First of all, there was a semicolon at the end. And most importantly,% w / w is not power . This operator makes bitwise xor . For power you can use ^ . The function comes from pow(b, 2) . Or much better: #include <math.h> .

  • b*b

    There were no relatives! It should be if delta < 0 .

  • if (delta < 0)

    Where does the raiz = sqrt(delta); function come from? You should include sqrt to make it work.

  • #include <math.h>

    printf ("O valor de X1 e X2 são: ", x1, x2); will not count the values for you. It also does not know how to detect the types of values you want to deal with. Use this:

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

    You have declared a printf function. It returns int main() , but you did not return int ! Its function is returning unspecified value, which can be problematic. Add at the end: int . Traidically zero indicates no error occurred. You can use one for error. Or any other value of your choice.

  • Arguments

    The function return 0; takes any number of arguments of any kind. This is illegal for the int main() function. It should either: get zero arguments, or get a main and a int . Set it like this to zero arguments:

    int main(void)
    
  • char**

    Only ASCII characters in C code are allowed. You have used things like stray '6' in program or ç . It is also likely that you have entered some invisible (nonprintable) 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 to the syntax of C. I suggest a more detailed study of how it works.

        
    11.04.2014 / 15:40
    -2

    Two common mistakes in new programmers are: Lack of attention and do not value good practice.

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

    no if (a = 0) You made 2 errors. One to use a ";" where it should not and another mistake 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 use only one line (not a block) in if / else you do not need "{}", it's good practice to always use them.

        
    10.04.2014 / 22:10