During run-time of a menu driven program, I want the user to see what option the user has entered. Something like this :-

[OUTPUT]
.
.
.
Enter Option : [1] <- [I]User given[/I]

Press Any Key To Continue ... [_] <- [I]Blinking Cursor[/I]
.
.
.
[/OUTPUT]

After I hit enter, the program will continue as usual.

My requirements

  • The code should be compiler independent
  • Should be a one-liner
  • Should not utilize a separate variable

The list is according to my priority. If No. 1 fails, No. 2 should not fail. If No. 2 fails, No. 3 should not fail. If No. 3 fails, then i have nothing more to say

I once tried something like this,

int n;
printf("Enter Any Number & Then Press Enter To Continue ... ");
scanf("%d", &n);

But this looks ugly and I had to use a separate variable.
So, in a nutshell, I'm looking for an alternate of the non-standard
getch();

Recommended Answers

All 7 Replies

>>But this looks ugly and I had to use a separate variable.

I think that looks beautiful. In fact you can just make a function that does that, and so it will be in a sense a one liner.

I don't think there is a standard implementation of getch in C

Ya, mine is a one-liner but it failed the last point on my list. So I was wondering whether there was any other alternative ...
Thanks for the "function" though ...

You do not actually have to save the variable anywhere:

printf("Enter Any Number & Then Press Enter To Continue ... ");
scanf("%*d");

Or this:

printf("Press Enter To Continue ... ");
scanf("%*[^\n]");

The first scanf() reads one integer and discards it. The second reads input from keyboard until newline is found, then discards everything.
http://linux.die.net/man/3/scanf

>So, in a nutshell, I'm looking for an alternate of the non-standard getch();
Unfortunately, you can't do it in one line without making some assumptions or rolling the whole thing up into a function. Assuming you want to convert this program:

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

int main ( void )
{
  puts ( "Hello, world!" );

  fputs ( "Press [Enter] to continue . . .", stdout );
  fflush ( stdout );
  getch(); /* Not portable, we want to replace this line */
  
  return 0;
}

This is the equivalent portable program:

#include <stdio.h>

int main ( void )
{
  puts ( "Hello, world!" );

  fputs ( "Press [Enter] to continue . . .", stdout );
  fflush ( stdout );
  getchar();
  
  return 0;
}

There are two immediate problems with the equivalent solution:

  1. If there are any characters in the stream, getchar won't block, but getch will. This is because getchar is a part of the stdio library (and is bound to the shell) while getch reads directly from the keyboard buffer.
  2. getch will return on any single character while getchar pretty much always requires the [Enter] key to be pressed. This is because of line-oriented input from the shell.

Problem 2 isn't really a problem since you stated that you want [Enter] to be the control key for this prompt, but problem 1 is still a big problem. Unless you're careful to keep your stream clear, you may need to flush the input stream first. I gave an example of portably flushing the stream in another thread, but here it is again:

void interactive_flush ( FILE *in )
{
  /*
    Don't flush the stream if it's in an "error" state because 
    flushing clears the state. It might be needed elsewhere
  */
  if ( !feof ( in ) && !ferror ( in ) ) {
    int ch;

    /* Line-oriented: a newline or EOF marks the end of interactive input */
    do
      ch = getc ( in );
    while ( ch != '\n' && ch != EOF );

    /* The above loop could put the stream in an "error" state */
    clearerr ( in );
  }
}

Stream I/O in C can be tricky in terms of extraneous characters and flushing. It's generally better to keep your interactive stream clean by reading entire lines (fgets or something that otherwise isn't gets) and parsing them internally. That way you don't have to worry about extra characters muxing up your input logic.

Okay, one last thing Narue, when i'm using the function, what parameter will I send the the function?
I was thinking stdin but I maybe wrong but I wanted to be sure(It worked though!)
Will it be something like this ->

printf("\n\n\tPress Enter To Continue ... ");
getchar();
interactive_flush(stdin);

>I was thinking stdin
The stream in question is whatever input stream you want to clean up in a line-oriented fashion, but yes, it was written with stdin in mind.

>Will it be something like this ->
Close:

printf("\n\n\tPress Enter To Continue ... ");
interactive_flush(stdin);
getchar();

The idea is for interactive_flush to clean up any extraneous characters in the stream. Note that the order you had won't work if there's more than one extra character. getchar will read the extra character, then interactive_flush will clear out the rest without blocking. The thing that causes the program to pause is the input request waiting (blocking) for the user to type something. If something is already there to read, the request won't block.

In the correct order, interactive_flush cleans out the extra characters and then getchar blocks.

Ya, it's worked. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.