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
CodePudding user response:
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.
CodePudding user response:
if (Input == 'D') //Only this 'if' match next 'else'.
{
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);
Replace these logics with switch/case
CodePudding user response:
With this code
if (Input == 'D')
{
/* */
}
else
printf("%c ist ein falscher Input!", Input);
you will get the message for anything not 'D'.
As comments already mentioned, you need to have a chain of
if(){}
else if(){}
else if(){}
else
{}
so that the condition on 'D' is only checked if all others have already failed and the last else is only used if neither 'D' nor anything before matches.
CodePudding user response:
You need to learn switch ... case which suits this much more.
int main(void)
{
float a;
float b;
float c;
char Input;
int valid = 0;
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);
valid = 1;
switch(Input)
{
case 'A':
c = a b;
break;
case 'S':
c = a - b;
break;
case 'M':
c = a / b;
break;
case 'D':
c = a * b;
break;
default:
valid = 0;
printf("%c ist ein falscher Input!", Input);
break;
}
if(valid)
{
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);
printf("Das Ergebnis ist: %f\n", c);
}
} while (Input != 'K');
printf("Programm Stop");
return 0;
}
