how to avoid wrong answers !!!

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;

}while(l == 'y' ||l == 'Y');

return 0;
}
Last edited on
Simply use a do-while:

1
2
3
4
do {
    cout << "would you like to try again (y or n)";
    cin >> l; 
} while(l != 'y' && l != 'n');
I tend to write a number of small functions for individual user inputs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cctype>
#include <ciso646>
#include <iostream>
#include <string>

bool GetYesOrNo( const std::string& prompt1, std::string prompt2 = "", bool fail = false )
{
  using namespace std;
  if (prompt2.empty()) prompt2 = prompt1;
  
  cout << prompt1;
  while (true)
  {
    string s;
    getline( cin, s );
    if (!cin) return fail;  // If input failure then there is no point in continuing here

    s.erase( 0, s.find_first_not_of( " \t" ) ); // Get rid of any leading spaces
    if (!s.empty())
      switch (toupper( s[0] ))
      {
        case 'Y': return true;
        case 'N': return false;
      }

    // User didn't answer the question. Ask again.
    cout << prompt2;
  }
}

int main()
{
  using namespace std;
  bool b = GetYesOrNo( "Are you happy? ", "Well, are you? " );
  if (!b)
    b = GetYesOrNo( "Will you tell someone what's wrong? " );

  if (!b) cout << "Sorry.\n";
  else    cout << "Yeah!\n";
}

Hope this helps.
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:

1
2
3
4
5
6
	do
	{
		std::cin >> l;

		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	} while(l != 'n');
i just want the calculator to take a different cin if l was any thing than y or n
@Renthalkx97
I think you misunderstand the logic.

minomic's answer continues the loop while the input is neither 'y' nor 'n' -- which is exactly the desired behavior.

Your loop requires the user to enter 'n' before it will let him out.
Yes, it is indeed a very simple and "standard" mechanism:

1
2
3
do {
    // ask something
} while ( /* you don't like the answer */ )
@Duoas
I was going by the context of the cout statement. It says
cout << "would you like to try again (y or n)"
; 'y' meaning continue the loop and 'n' meaning break out of the loop. Isn't that what the OP wanted?

EDIT

I didn't actually read the entire question, sorry. I assumed that he wanted to quit if 'n' was the input else keep doing his calculations. My bad.
Last edited on
Yet you make a very good point. I didn't consider that there might be anything else in the loop. (Which is a valid assumption.)

My bad too.

[edit] My brain must be addled.
Last edited on
Topic archived. No new replies allowed.