Taschenrechner: was ist falsch?
-
Meine Version eines ersten Taschenrechnerprogramms gibt unnverständliche Zahlen aus. Weder verstehe ich die Logik hinter den Outputs, noch, wie das Problem zu beheben ist.
#include <iostream>
using namespace std;int main ()
{int a;
char b;
int c;cin >> a;
cin >> b;
cin >> c;if (b == '+') {
cout << a+b << endl;
}if (b == '-') {
cout << a-b << endl;
}if (b == '') {
cout << ab << endl;
}if (b == '/') {
cout << a/b << endl;
}return 0;
}Irgendjemand eine Idee?
-
Spielen wir dein Programm doch mal durch.
Eingabe: 4+2
Erste if prüft '+' == '+' => true also Körper ausführen
Da steht: Ausgabe 4 + '+'Was erwartest du da?
-
a, b und c sind blöde Namen für Variablen, wie du ja jetzt dank Schlangenmensch gesehen hast.
-
Ok, danke. Ich hab fälschlicherweise b statt c geschrieben.
Es sollte
a+c statt a+b
a-c statt a-b
a*c statt a*b
a/c statt a/bheißen.
Blöd, dass ich das übersehen habe...