Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie input quesition

Status
Not open for further replies.

sos

IS-IT--Management
Apr 10, 2000
53
CA
I have been trying to figure out how to do something like:

"Press any key to continue" or "Hit 's' to continue" or something similar in C.

Is there a function like getchar() for example that does not wait for the newline char? What do i need to include to use it?

Or do i need to figure out keyboard interrupts or something?

thanxInAdvance

SOS C N00813
 
Hi 'sos'.
The function you could use is 'getch()' which is defined in the 'conio.h' header. It basically waits for user to press a key and returns the value of the key pressed. The code below illustrates its use.
Code:
#include <stdio.h>
#include <conio.h>

int main(void)
{
  printf(&quot;Press 'X' to exit&quot;);
  while(getch() != 'X')
    ;
  printf(&quot;\nEnd of program\n&quot;);
  return(0);
}
A point to note is value returned is case sensitive, ie. in the code above only capital x will end program.
Hope that helps. Adonai :)
 
Hmmm ok that is helpful...thank u for your reply..

I think i already tried that function thought and i did not have it....i am using the gcc compiler that comes with mandrake 9.0...i haven't installed any extra lib or anything does anyone know what i need to install to get this function to work...???

I will try getch in my windows 2000 ide thanx again...
 
I think you will find that under Linux the getch() and related functions are not in stdio.h but are part of the ncurses library. You'll need to include the ncurses.h header file, the ncurses library and use the concept of ncurses windows for your input.

Cheers - Gavin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top