0

I am trying to write a program in C where I intend to control the execution of a while loop according to user input in stdin. I am able to write the program in three different ways as following which perform as intended:

Using scanf() function

#include<stdio.h>
#include<string.h>

int main()
{
 char loop= 'y';
 while(loop=='y')
    {
     printf("Do you want to continue? [y|n]: ");
     scanf("%c", &loop);

     while(getchar() != '\n');
     if(loop != 'y' && loop != 'n')
        {
         printf("Error! Please enter 'y' or 'n' \n");
         loop = 'y';
        }
    }
 return 0;
}

Using fgets() function

#include<stdio.h>
#include<string.h>

int main()
{
 char loop[5]= "yes\n";

 printf("%s",loop);
 while(strcmp(loop,"yes\n")==0)
    {
     printf("Do you want to continue? [yes|no]: ");
     if(strcmp(loop,"yes\n")!=0 && strcmp(loop,"no\n")!=0)
        printf("Error! Please Enter 'yes' or 'no'\n");
     else
        fgets(loop,5,stdin);
    }
 return 0;
}

Using getch() from conio.h

#include<stdio.h>
#include<string.h>
#include<conio.h>

int main()
{
 char loop= 'y';
 while(loop=='y')
    {
     printf("Do you want to continue? [y|n]: ");

     loop = getch();
     printf("\n");
     if(loop != 'y' && loop != 'n')
        {
         printf("Error! Please enter 'y' or 'n' \n");
         loop = 'y';
        }
    }
 return 0;
}

Although all the aforementioned methods perform the task of stopping or continuing the while loop according to user input, the getch() is little different. Using getch(), the user does not have to press the enter after the user inputs y or n and the program terminates or continues as soon as the user provides input.


I am aware that conio.h is not included in the standard gcc library and is frowned upon to use. However, I would like to implement the functionality of getch() using either scanf() or fgets() or any other standard library function. By functionality of getch(), I mean that as soon as a user press the desired key on terminal, the while loop should terminate without needing to press the enter by the user.

Is it possible for either fgets() or scanf() to do this task?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Pankaj
  • 519
  • 2
  • 5
  • 20
  • 6
    Short story: You can't, because the `getch` function bypasses `stdio` and reads more directly from the console. Long story: You can disable buffering on `stdin` which *might* help solve your problem (and then I recommend using [`getc` or `fgetc`](http://en.cppreference.com/w/c/io/fgetc) instead). No guarantees though. – Some programmer dude Jun 02 '17 at 03:45
  • BTW The `fgets` version is incorrect. – BLUEPIXY Jun 02 '17 at 03:46
  • Oh, and you should *really* do some error checking for the input functions. All the functions mentioned (by you and me) all have return values that should be checked for errors or other problems. – Some programmer dude Jun 02 '17 at 03:46
  • @BLUEPIXY what is wrong in fgets() version? In my system, it is giving the desired output. – Pankaj Jun 02 '17 at 03:47
  • @Someprogrammerdude Thanks for the comment. I have indeed done some error checking in all the programs. Is that not enough? – Pankaj Jun 02 '17 at 03:49
  • What if the user presses the EOF sequence instead of writing anything? You never check for that. – Some programmer dude Jun 02 '17 at 03:50
  • 1) Can't compile `return0;`. 2) `if(strcmp(loop,"yes\n")==0 && strcmp(loop,"no\n")==0)` is meaningless. Specifically, the loop can not be ended with `no`. – BLUEPIXY Jun 02 '17 at 03:50
  • `if(strcmp(loop,"yes\n")==0 && strcmp(loop,"no\n")==0)`??? – ad absurdum Jun 02 '17 at 03:50
  • `if(strcmp(loop,"yes\n")==0 && strcmp(loop,"no\n")==0)` - This is to force the user to enter either 'yes' or 'no' otherwise report an error. – Pankaj Jun 02 '17 at 03:52
  • You do not understand what you are talking about. – BLUEPIXY Jun 02 '17 at 03:53
  • @DavidBowling Yes, I do know it. But I agree with others that fgets() version is wrong. I am now trying to fix it. Sorry for the trouble. – Pankaj Jun 02 '17 at 04:01
  • @DavidBowling I have now edited the fgets() version of the code. It now works correctly though I should have included the error checking. – Pankaj Jun 02 '17 at 04:05
  • Guys, now that from the discussion in the comments, it is clear that getch() cannot be implemented via `scanf()` or `fgets()` though some easy means, do you suggest that I should rollback this question? I guess that it might not be useful for any one. Please respond. – Pankaj Jun 02 '17 at 04:08
  • @Pankaj _It now works correctly_ no. It is still not correct. Try Enter `end`. – BLUEPIXY Jun 02 '17 at 04:10
  • Code still is not quite behaving as expected; try to enter "maybe". – ad absurdum Jun 02 '17 at 04:10
  • 1
    Even though the question itself is obvious for most people, there may be people who want to know it. You can post your conclusions as an answer yourself. – BLUEPIXY Jun 02 '17 at 04:13
  • 1
    See also: https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux/; https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed/; https://stackoverflow.com/questions/1513344/how-to-get-the-last-key-pressed-without-stopping-the-c-program; and no doubt many others. – Jonathan Leffler Jun 02 '17 at 04:36

0 Answers0