PDA

View Full Version : getchar() running to infinte loop,its not responding when i run it.



keethi2
8th September 2011, 21:55
Hi,

I'm using getchar() in qt-creator4 on linux-platform.Its going to an infinte loop,im not even able to enter the character,the system hangs so i force quit.Could someone please help me,how i can use getchar() in qt-creator4 on linux ?

sample code:


#include <iostream>
#include <stdio.h>

int main()
{
char ch;
printf( "Enter a character : ");
ch=getchar();
printf("Char is %c \n",ch);
return 0;
}

Actually i'm supposed to use getch(),but since getch() is not available in linux.I had to use getchar(). It would great ,if anyone could suggest me how i
can use getch() in linux ? Along with the solution to the above problem.

Thanks,
Keerthi

wysota
8th September 2011, 22:34
Nothing hangs, press enter/return. And when the program exits, type in "man getchar" and press enter again and start reading.

ChrisW67
8th September 2011, 22:58
Actually i'm supposed to use getch(),but since getch() is not available in linux.
I think you are mistaken:


$ man getch
curs_getch(3X) curs_getch(3X)

NAME
getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get (or push back) characters from curses
terminal keyboard
...

You need to include the relevant curses header, initialise and cleanup curses, and link the curses library.

If you intend turning this into a program that uses Qt then you almost certainly don't want either function.