When I run the code it stops after entering the second number. I'm pretty sure I have a logic error somewhere but I can't seem to find it. Also this is my first try using functions, maybe I did something wrong there?
This is my code:
#include <iostream>
using namespace std;
/*Variables*/
double d_Zahl1 = 0.0;
double d_Zahl2 = 0.0;
double d_Ergebnis = 0.0;
char c_Operator = ' ';
/*Functions*/
double add(double d_Zahl1, double d_Zahl2)
{
d_Ergebnis = d_Zahl1 + d_Zahl2;
return d_Ergebnis;
/*Output of result*/
cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};
double substract(double d_Zahl1, double d_Zahl2)
{
d_Ergebnis = d_Zahl1 - d_Zahl2;
return d_Ergebnis;
/*Output of result*/
cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};
double divide(double d_Zahl1, double d_Zahl2)
{
d_Ergebnis = d_Zahl1 / d_Zahl2;
return d_Ergebnis;
/*Output of result*/
cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};
double multiply(double d_Zahl1, double d_Zahl2)
{
d_Ergebnis = d_Zahl1 / d_Zahl2;
return d_Ergebnis;
/*Output of result*/
cout << "\n\n\n" << d_Zahl1 << c_Operator << d_Zahl2 << '=' << d_Ergebnis;
};
/*Main function*/
int main()
{
/*Output of head*/
cout << "\t----------Calculator----------\n\n";
/*Input of 1. number*/
/*checking if the input is a number*/
while (cout << "\n\nPlease enter your first number: " && !(cin >> d_Zahl1))
{
cout << "\nThat's not a valid input. Try again. " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
/*Einlesen des Operanden*/
cout << "\nPlease enter your operator (+,-,*,/): ";
do { //Fehler durch do-while Schleife abfangen
cin >> c_Operator;
switch (c_Operator) {
case '+':
/*Eingabeaufforderung für zweite Zahl*/
cout << "\nPlease enter your second number: ";
/*Einlesen der 2. Zahl*/
cin >> d_Zahl2;
add(d_Zahl1, d_Zahl2);
break;
case '-':
/*Eingabeaufforderung für zweite Zahl*/
cout << "Please enter your second number: ";
/*Einlesen der 2. Zahl*/
cin >> d_Zahl2;
substract(d_Zahl1, d_Zahl2);
break;
case '*':
/*Eingabeaufforderung für zweite Zahl*/
cout << "Please enter your second number: ";
/*Einlesen der 2. Zahl*/
cin >> d_Zahl2;
multiply(d_Zahl1, d_Zahl2);
break;
case '/':
/*Eingabeaufforderung für zweite Zahl*/
cout << "Please enter your second number: ";
/*Einlesen der 2. Zahl*/
cin >> d_Zahl2;
divide(d_Zahl1, d_Zahl2);
break;
default:
cout << "-----Wrong input!-----\n\n\n\n\n\n\n\n\nPlease enter your operator (+,-,*,/): ";
}
} while (c_Operator != '+' || c_Operator != '-' || c_Operator != '*' || c_Operator != '/'); /*Solange keines der Rechenzeichen --> repeat*/
system("pause");
return 0;
}
I wanted it to go like:
Enter 1. Number: 5
Enter Operator: +
Enter 2. Number: 5
5 + 5 = 10
but right now its just:
Enter 1. Number: 5
Enter Operator: +
Enter 2. Number: 5