Okay so I am still way of a beginner at c++. I have been attempting to code a decent basic calculator in order to test my basics and what I know. I can not for the life of me understand why the program does not restart through loop and just continues to end every time, whether I input "Yes", "yes", "Y", or "y". Here is what I have:
#include <iostream>
using namespace std;
int main()
{
int a, b;
string c, d;
while(c != "+", "-", "*", "/" or c == "Yes", "yes", "Y", "y") {
cout << "N1 = "; cin >> a; cout << endl;
if(!cin)//If anything but integer given, program ends
{
cout << "Give a Number next time.. " << endl;
return 0;
}
cout << "N2 = "; cin >> b; cout << endl;
if(!cin)
{
cout << "Give a Number next time.. " << endl;
return 0;
}
cout << "+ , - , * or / ? "; cin >> c; cout << endl;
if(!cin)
{
cout << "Give a Number next time.. " << endl;
return 0;
}
if( c == "+")//Operator Functions from input
{
cout << a + b << endl;
}
else if( c == "-")
{
cout << a - b << endl;
}
else if( c == "*")
{
cout << a * b << endl;
}
else if( c == "/")
{
cout << a / b << endl;
}
if(c != "+ , -, * , /")//if not operator, Try again?
{
cout << "Again?... Y/N? " << endl;
cin >> c;
}
if(c == "Yes", "yes", "Y", "y")
{
cout << "Restarting.. " << endl;
}
while(c == "No", "no", "N", "n")
{
cout << "closing... " << endl;
return 0;
}
}
I have also tried changing the very last while to an if statement but neither works. Just hoping to see what I am misunderstanding. Thanks! Maybe even some tips to shorten my code or make it simpler would be much obliged as well!
Aucun commentaire:
Enregistrer un commentaire