my calculator is so simple but at the end of it i asks the user if he want to try again what i want to do is that if the user enter y the program will restart if he enter n the program will close but what if he enter "sdaidai" i want to told him that it is wrong input please try again and forced him to enter y or n
#include <iostream>
#include <string>
using namespace std;
int main ()
{
double x, y ,s,d,p;
char j,l;
cout<<"welcome to modern Academy simple calculator "<<endl;
do{
cout<< "enter the first number :";
cin>>x;
cout<<"enter the second number :";
cin>>y;
cout<<"enter the symbol :";
cin>>j;
s=x+y;
d=x-y;
p=x*y;
if(j== '+'){
cout <<"the result is : "<<s<<endl;
}else if (j=='-'){
cout<<"the result is : "<<d<<endl;
}else if(j=='*'){
cout <<"the result is : "<<p<<endl;}
else if(j=='/'){
if(y==0)
{
cout<<"not valid"<<endl;
}else{
cout<<"the result is : "<<(x/y)<<endl;}}
cout << "would you like to try again (y or n)";
cin >> l;
That doesn't do what you think it does. You're saying that if l doesn't equal 'y' and l doesn't equal 'n' then exit the loop. Well what else could it be besides 'y' or 'n'? It will exit regardless of you typing 'y'. In order to correct this you need to do this: