Ad

If-else In Do-while In C

- 1 answer

I have tried to code a small calculator. The user has to decide whether he wants to add, subtract, multiplicate or divide two numbers. This is probably a noob mistake since this is the second day i learn programming but here we go Here is the Code:

#include <stdio.h>
#include <stdbool.h>



int main()
{
    float a;
    float b;
    float c;
    char Input;


    printf("Herzlich Wilkommen, ");


    do
    {
        printf(" \nWas moechten sie tun?\n Fuer Addition A \n Fuer Subtraktion S \n Fuer Division D \n Fuer Multiplikation M\n");
        printf(" Zum Beenden 0 druecken!\n");
        printf(" Eingabe:");
        scanf(" %c", &Input);

        if (Input == 'A')
        {
            printf("Geben sie Zahl 1 an:");
            scanf(" %f", &a);
            printf("Zahl 1: %f\n", a);
            printf("Geben sie Zahl 2 an:");
            scanf(" %f", &b);
            printf("Zahl 2: %f\n", b);
            c = a + b;
            printf("Das Ergebnis ist: %f\n", c);
        }
        if (Input == 'S')
        {
            printf("Geben sie Zahl 1 an:");
            scanf(" %f", &a);
            printf("Zahl 1: %f\n", a);
            printf("Geben sie Zahl 2 an:");
            scanf(" %f", &b);
            printf("Zahl 2: %f\n", b);
            c = a - b;
            printf("Das Ergebnis ist: %f\n", c);
        }
        if (Input == 'M')
        {
            printf("Geben sie Zahl 1 an:");
            scanf(" %f", &a);
            printf("Zahl 1: %f\n", a);
            printf("Geben sie Zahl 2 an:");
            scanf(" %f", &b);
            printf("Zahl 2: %f\n", b);
            c = a / b;
            printf("Das Ergebnis ist: %f\n", c);
        }
        if (Input == 'D')
        {
            printf("Geben sie Zahl 1 an:");
            scanf(" %f", &a);
            printf("Zahl 1: %f\n", a);
            printf("Geben sie Zahl 2 an:");
            scanf(" %f", &b);
            printf("Zahl 2: %f\n", b);
            c = a * b;
            printf("Das Ergebnis ist: %f", c);
        }
        else
            printf("%c ist ein falscher Input!", Input);
        
    } while (Input != 'K');

    printf("Programm Stop");
    return 0;
}

The problem is that the else funktion is also printed after a calculation was succesfully done. This is the output:

Herzlich Wilkommen,
Was moechten sie tun?
 Fuer Addition A
 Fuer Subtraktion S
 Fuer Division D
 Fuer Multiplikation M
 Zum Beenden K druecken!
 Eingabe:A
Geben sie Zahl 1 an:12
Zahl 1: 12.000000
Geben sie Zahl 2 an:12
Zahl 2: 12.000000
Das Ergebnis ist: 24.000000

A ist ein falscher Input!

Was moechten sie tun?
 Fuer Addition A
 Fuer Subtraktion S
 Fuer Division D
 Fuer Multiplikation M
 Zum Beenden K druecken!
 Eingabe:

I am referring to the line that say "A ist ein falscher Input". Why is it putting that line out? It seems like the programm thinks that A is still in the Input which would be logical to me. But why is it not putting out the function that has 'A' as a condition? I hope you could understand what my problem is since english is not my first language! Thank you

Ad

Answer

You have 3 separate if statements:

    if (Input == 'A')
    {
       ...
    }
    if (Input == 'S')
    {
       ...
    }
    if (Input == 'M')
    {
       ...
    }

Followed by an if/else:

    if (Input == 'D')
    {
       ...
    }
    else
        printf("%c ist ein falscher Input!", Input);

So the else part here will run if Input is not "D".

You want to chain the separate if statements into if/else:

    if (Input == 'A')
    {
       ...
    }
    else if (Input == 'S')
    {
       ...
    }
    else if (Input == 'M')
    {
       ...
    }
    else if (Input == 'D')
    {
       ...
    }
    else
        printf("%c ist ein falscher Input!", Input);

This way only one of the conditions will run.

Ad
source: stackoverflow.com
Ad