2

I am doing program in c. I wanted to scan my input like :

[root@localhost sudoers.d]# cd /home/Hitesh/
Display all 113 possibilities? (y or n)

Here in bash,when user enters 'n' or 'y' it directly come on prompt.

But in c program if I am taking input through getchar() and then if I press 'n' or 'y',I explicitly have to press enter key after that to resume execution.

So I want,my execution resume as soon I press 'n',it should not wait for enter key press.

How can I achieve this in c programming.

4

3 回答 3

0

您可以使用 getch() 代替 getchar()。getch() 只接受一个字符,所以你不想按回车键。

char opt = getch();
// show typed value
printf("%c", opt );
if( opt == 'y' )
{
  ............
}
else 
{
  ............
}

希望这会有所帮助

于 2013-03-04T10:18:02.670 回答
0

使用getch()

char answer = getch();
if(answer=='y')
   //yes!
else
   //no!
于 2013-03-04T10:11:53.553 回答
0

您正在寻找的功能是getch ()

于 2013-03-04T10:06:40.207 回答