0

I want to get only y and n from user to enter
when user try to enter another it get the input again until user enter
"y" or "n"
i use this logic but its not working

do
{
   cout<<"\n\n Enter Number: ";
   number=getche();
   cout<<"\n Want to enter another:(y/n)";
   ans=getche();
      if(ans!='n'||ans!='y')
        ans=getch();
}
  while(ans!='n');
4
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by xofth
0

Put in a cin.ignore() after the ans = getche(); line. The newline ('\n') from the when you entered y or n the first time was staying in the input stream and getting picked up by the getch() which took it as input and moved on. I don't know offhand if the cin.ignore() plays nice with the conio functions.

That being said, I would avoid the conio.h functions unless it's absolutely necessary to do so. Use cin.get() or getchar() (getchar requires <cstdio>) for portability.

0

Put in a cin.ignore() after the ans = getche(); line. The newline ('\n') from the when you entered y or n the first time was staying in the input stream and getting picked up by the getch() which took it as input and moved on. I don't know offhand if the cin.ignore() plays nice with the conio functions.

That being said, I would avoid the conio.h functions unless it's absolutely necessary to do so. Use cin.get() or getchar() (getchar requires <cstdio>) for portability.

There was no \n left in the buffer unless it was before the do-while . getche() is the same as getch() with echo.

But, I agree with jonsca about not using conio.h functions. They are non-standard and not used in the industry.

Looking at your IF statement, put it in a truth table. You will find it will always be true.

0

Here was my interpretation (I reserve the right to be wrong, so I'm just seeking for my own clarification here, so I'll beg the OPs pardon)

ans=getche(); //user inputs 'g' <enter> (only room for g in the 'ans')
if(ans!='n'||ans!='y')   //this is true
        ans=getch(); //I was saying enter gets picked up by this
                //apparently that's not the case?

On a peripheral matter, I thought this was funny (from the conio.h of a recent mingw):

/*
 * conio.h
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is a part of the mingw-runtime package.
 * No warranty is given; refer to the file DISCLAIMER within the package.
 *
 * Low level console I/O functions. Pretty please try to use the ANSI
 * standard ones if you are writing new code.
 *
 */

(my color emphasis added)
You can't make this stuff up...

Edited by jonsca: n/a

0

my question is still there that how can i place check on the input
that user can enter only 'y' or 'n'

0

my question is still there that how can i place check on the input
that user can enter only 'y' or 'n'

Consider using logical AND (&&) ..

do
{
  prompt the user ..
  get the input ..
}
while(ans != 'y' && ans != 'n');

or logical OR (||) ..

do
{
  prompt the user ..
  get the input ..
}
while((ans == 'y' || ans == 'n') == false);

Try to think through your original if-statement to understand why it will not work.

0

I got the answer

do
{
cout<<"\n\n Enter Number: ";
number=getche();
cout<<"\n Want to enter another´┐╝y/n)";
ans=getche();
while(ans!='n'||ans!='y')
{
cout<<"enter only y or n";
ans=getche()
if(ans=='n'||ans=='y')
break;
}
}
while(ans!='n');
0

Basically, a thing that you need to understand is the fact that

while( ans != 'n' || ans != 'y' )

is effectively the same as writing

while( true )

And there is no way around that. :)

0

I got the answer

But your ending loop is too complex.

while(ans!='n'||ans!='y')
{
  cout<<"enter only y or n";
  ans=getche()
  if(ans=='n'||ans=='y') 
     break;
}

If the while statement is supposed to exit when Y or N is pressed, why the if statement that breaks out of the loop? Get rid of the if statement and let the while do it's job.

Basically, a thing that you need to understand is the fact that

while( ans != 'n' || ans != 'y' )

is effectively the same as writing

while( true )

And there is no way around that. :)

That was why I suggested a Truth Table... :icon_wink:

Here was my interpretation (I reserve the right to be wrong, so I'm just seeking for my own clarification here, so I'll beg the OPs pardon)

ans=getche(); //user inputs 'g' <enter> (only room for g in the 'ans')
if(ans!='n'||ans!='y')   //this is true
        ans=getch(); //I was saying enter gets picked up by this
                //apparently that's not the case?

No, getche() and getch() work the same way. Hit a key and the program continues. You won't get a chance to hit the ENTER.

Edited by WaltP: n/a

0

That was why I suggested a Truth Table... :icon_wink:

I know you did, but it just so much seems that the point still needs to be made, so to speak...

0

No, getche() and getch() work the same way. Hit a key and the program continues. You won't get a chance to hit the ENTER.

Sorry, when I wrote "enter" on line 3, I meant the stray newline in the stream from line 1. I may still missing something obvious, it's not intentional I promise.

0
do
{
cout<<"\n\n Enter Number: ";
number=getche();
cout<<"\n Want to enter another´┐╝y/n)";
ans=getche();
while(ans!='n'||ans!='y') // this while loop get the input again and again until the n or y are pressed
{
cout<<"enter only y or n";
ans=getche()     // if we dont again get the input again the loop continues
if(ans=='n'||ans=='y') // this is the check on the 2nd input that brings the control out from the loop
break;
}
}
while(ans!='n');

any other way u have??
plz write the code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.