Can somebody help me, why this aren't working?
I always get: else without a previous if
PS: I'm from Austria, sorry for my bad English :)

#include <stdio.h>

int main(void)
{
    int w;
    double v, s, t;

    printf("Dieses Programm berechnet Geschwindigkeiten, Wegdifferenzen und\n"
            "Zeitdifferenzen fuer eine gleichfoermige Translation.\n"
            "1 ... Berechnung der Geschwindigkeit\n"
            "2 ... Berechnung der Wegdifferenz\n"
            "3 ... Berechnung der Zeitdifferenz\n");
    printf("Bitte waehlen Sie aus:"); scanf("%d", &w); getchar();

    if  (w = 1)             printf("Wegdiffernz (in m):\n");        scanf("%lf", &s);   getchar(); 
                            printf("Zeitdiffernz (in s):\n");       scanf("%lf", &t);   getchar(); 
                            (v = s / t);
                            printf("Ergebnis:\n Geschwindigkeit: %lf m/s", &v);

    else if  (w = 2)        printf("Geschwindigkeit (in m/s):\n");  scanf("%lf", &v);   getchar();
                            printf("Zeitdiffernz (in s):\n");       scanf("%lf", &t);   getchar();
                            (s = v * t);
                            printf("Ergebnis:\n Wegdifferenz: %lf m", &s);

    else if (w = 3)         printf("Geschwindigkeit (in m/s):\n");  scanf("%lf", &v);   getchar();
                            printf("Wegdiffernz (in m):\n");        scanf("%lf", &s);   getchar();
                            (t = v * s);
                            printf("Ergebnis:\n Zeitdifferenz: %lf s", &t);             
    return 0;
}

Dani AI

Generated

A short diagnosis and a few concrete fixes tied to the replies already in this thread.

As pointed out, the compiler error "else without a previous if" is caused by missing braces: in C an if without {} controls only the single next statement, so any extra statements placed between that statement and the else break the if/else pairing. supplied a braced layout (good), but a couple of logic/IO bugs remained in the posted code: the branches use assignment (if (w = 1)) instead of comparison (if (w == 1)), printf calls pass addresses (e.g. &v) instead of the value, and the time formula for option 3 is wrong — it should be t = s / v, not t = v * s.

Correct comparison, printf/scanf usage, and a basic safety check:

/* illustrative fragment */
if (w == 1) {
    if (scanf("%lf %lf", &s, &t) == 2 && t != 0.0) {
        v = s / t;
        printf("Geschwindigkeit: %f m/s\n", v);
    }
}

Better structure and robustness: prefer a switch for menu selection, always check the return value of scanf (or use fgets + sscanf/strtod for safer input), and test divisors before dividing. A compact control-flow skeleton:

switch (w) {
case 1: /* read s,t; if t!=0 then v = s/t */ break;
case 2: /* read v,t; s = v*t */ break;
case 3: /* read v,s; if v!=0 then t = s/v */ break;
default: printf("Invalid selection\n");
}

Quick checklist for correctness (matches the fixes above): add braces around multi-line branches; use == for comparisons; pass values (not &value) to printf; use %lf with scanf and %f with printf; validate scanf results; check for division by zero; enable compiler warnings (for example gcc -std=c11 -Wall -Wextra -pedantic) to catch suspicious assignments and format mismatches. reported the braces fixed the compile error; the remaining items above will make the program correct and robust.

Recommended Answers

All 4 Replies

Hi, Hermelix welcome here at Daniweb! :)
Der Danny von Belgien!
Your main problem at frist glance, is a lack of curly braces.
You can omit them if your if statement only has one line.
If you have many lines you have to use them.
I ALWAYS use them

if (condition)
{
line1;
line2; etc.
}
else if (condition)
{
and so on
}

Hope it helps.

Here's your code, formatted and with curly braces as ddanbe explained.

    #include <stdio.h>

    int main(void)
    {
        int w;
        double v, s, t;

        printf("Dieses Programm berechnet Geschwindigkeiten, Wegdifferenzen und\n"
        "Zeitdifferenzen fuer eine gleichfoermige Translation.\n"
        "1 ... Berechnung der Geschwindigkeit\n"
        "2 ... Berechnung der Wegdifferenz\n"
        "3 ... Berechnung der Zeitdifferenz\n");
        printf("Bitte waehlen Sie aus:"); scanf("%d", &w); getchar();

        if (w = 1)
        {    
            printf("Wegdiffernz (in m):\n");        
            scanf("%lf", &s);   
            getchar(); 
            printf("Zeitdiffernz (in s):\n");      
            scanf("%lf", &t);   
            getchar(); 
            (v = s / t);
            printf("Ergebnis:\n Geschwindigkeit: %lf m/s", &v);
        }

        else if (w = 2)
        {    
            printf("Geschwindigkeit (in m/s):\n");  
            scanf("%lf", &v);   
            getchar();
            printf("Zeitdiffernz (in s):\n");       
            scanf("%lf", &t);   
            getchar();
            (s = v * t);
            printf("Ergebnis:\n Wegdifferenz: %lf m", &s);
        }

        else if (w = 3)
        {    
            printf("Geschwindigkeit (in m/s):\n");  
            scanf("%lf", &v);   
            getchar();
            printf("Wegdiffernz (in m):\n");        
            scanf("%lf", &s);   
            getchar();
            (t = v * s);
            printf("Ergebnis:\n Zeitdifferenz: %lf s", &t);
        }

        return 0;
    }

Big thank you, now it work's :)

Hermelix, mark this thread as solved please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.