Error 'Else' without a Previous 'if'

Asked

Viewed 5,814 times

-1

I got a problem and I don’t know how to fix it.

The program in C that I developed this presenting several types of errors and do not know what could be:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
    int SA, SN;
    int NOME, AUMENTO;
    printf ("NOME");
    scanf ("%f", &NOME);
    if (SA <= 400);
    SN= SA*1.12;
    AUMENTO="15%";
    else
    if (SA <=700);
    SN=SA*1,12;
    AUMENTO= "12%";
    else
    if (SA <=1000);
    SN=SA*1,1
    AUMENTO= "10%";
    else
    if (SA <= 1800);
    SN=SA*1,07;
    AUMENTO= "7%";
    else
    if (SA <=2500)
    SN=SA*1,04;
    AUMENTO= "4%";
    else
    AUMENTO="sem aumento"
    printf ("NOME: ", NOME, ",% de aumento: ", AUMENTO, ",Salario atual", SA, "Novo salário: ",SN);
    System ("PAUSE");

inserir a descrição da imagem aqui

  • I’m confused about one thing, because he’s making a mistake in the lines I’m using %?

  • It has a simple technique to discover the place of mistakes, called "Divide and Conquer". You do so, comments the second half of the code, checks whether errors continue, whether they continue to comment on the second half of the code that remained and so on, until you find the excerpt with a compilation error. For C++ this technique should provide the characteristics of the OO implementation.

3 answers

2

Your code is not with correct block syntax, missing keys to assign code sequence.

Example:

if (SA <= 400){
  SN= SA*1.12;
  AUMENTO="15%";
}else{
  //continuando seus if...
}

That’s why he makes a mistake saying that his LSE, elseif has no correspondent

  • I’ve corrected that thanks for pointing that out to me.

2


Corrected code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
    float SA, SN;
    char NOME[30], AUMENTO[10];
    printf ("NOME");
    gets(NOME);
    printf("Informe SA: ");
    scanf("%f", &SA);
    if (SA <= 400) {
        SN = SA*1.15;
        strcpy(AUMENTO, "15%");
    }
    else
        if (SA <= 700) {
            SN = SA*1.12;
            strcpy(AUMENTO, "12%");
        }
        else
            if (SA <=1000) {
                SN = SA*1.1
                strcpy(AUMENTO, "10%");
            }
            else
                if (SA <= 1800) {
                    SN = SA*1.07;
                    strcpy(AUMENTO, "7%");
                }
                else
                    if (SA <= 2500) {
                        SN = SA*1.04;
                        strcpy(AUMENTO, "4%");
                    }
                    else {
                        SN = SA;
                        strcpy(AUMENTO, "sem aumento");
                    }
    printf ("NOME: %s, %% de aumento: %s, Salario atual: %.2f, Novo salário: %.2f\n", NOME, AUMENTO, SA, SN);
    system ("PAUSE");
    return 0;
}

But note that it could get much simpler, for example:

#include <stdio.h>
#include <stdlib.h>
int main() {
    float SA, SN;
    char NOME[30];
    int AUMENTO;
    printf ("NOME");
    gets(NOME);
    printf("Informe Salário Atual: ");
    scanf("%f", &SA);
    if (SA <= 400)
        AUMENTO = 15;
    else
        if (SA <= 700)
            AUMENTO = 12;
        else
            if (SA <=1000)
                AUMENTO = 10;
            else
                if (SA <= 1800)
                    AUMENTO = 7;
                else
                    if (SA <= 2500)
                        AUMENTO = 4;
                    else
                        AUMENTO = 0;
    SN = SA * (float) (100 + AUMENTO) / 100;
    printf ("NOME: %s, %% de aumento: %d, Salario atual: %.2f, Novo salário: %.2f\n", NOME, AUMENTO, SA, SN);
    system ("PAUSE");
    return 0;
}
  • thanks you do not know how I tried to fix these my mistakes, with a while I get better.

  • 1

    Ugh! Looks like Python :)

1

Taking a first glance I notice that the code has lots of errors related to missing point-and-comma in several lines, and excess in the lines with *if*s. Also missing braces in if blocks.

  • Quite interesting your answer, but when so you can take the code he posted and fix it and send it back to him..

  • That should be a comment and not a response.

  • Ah, okay, okay, I’m sorry. I’m a rookie around here, I’m still getting the hang of it.

Browser other questions tagged

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