LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2009, 03:17 AM   #1
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 0
Question why cann't the getchar() return?


hi all
i just want to get a char from getchar(),but the getchar never return regardless how many characters i input and hit enter

here is the program i encounter the problem:

Code:
int main(int argc, char **argv){
    int qid;
    int pid;
    int i;
    key_t key;
    int len;
    struct message msg;
    struct message childMsg;
    char sel;
    if((key=ftok(".",'a')) == -1){
        perror("ftok");
        exit(1);
    }
    if((qid=msgget(key,IPC_CREAT|0666)) == -1){
            perror("msgget");
            return -1;
    }
    printf("get msgQueue:%d\n",qid);
    for(i=0; i<5; i++){
        puts("please enter the message to the queue");
        if((fgets(msg.msg_text,BUFSZ,stdin))==NULL){
            perror("fgets");
            exit(1);
        }
        msg.msg_type = i+1;
        len = strlen(msg.msg_text);
        if(msgsnd(qid,&msg,len,0) < 0){
            perror("message send");
            exit(1);
        }
    }
    printf("please select the message id you want to get:\n");

    for(i=0; i<5; i++){
            sel = getchar();
            if(msgrcv(qid,&childMsg,BUFSZ,sel-'a'+1,0)<0){
                perror("msgrcv");
                exit(1);
            }
            printf("the message is %s\n",childMsg.msg_text);
        }
        exit(0);
}
when the program run to getchar(), i input a char and hit enter, but i doesn't return....

what's the problem? thanks in advance
 
Old 08-13-2009, 06:49 AM   #2
jeromeNP7
Member
 
Registered: Jun 2009
Posts: 101

Rep: Reputation: 19
Try switching to scanf to get input within the program. Getchar isn't really waiting for any input and it will continue right away even if no input is found immediately. Scanf will wait for input.

Linux

Last edited by jeromeNP7; 09-04-2009 at 09:34 PM.
 
Old 08-13-2009, 07:10 AM   #3
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
i'll have a try with scanf
but i still wonder why getchar doesnot work as we want
 
Old 08-14-2009, 01:28 AM   #4
ebd
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Original Poster
Rep: Reputation: 0
everything goes well afer memseting childMsg everytime before msgrcv
thank you all
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getchar() satellite Programming 10 12-24-2017 01:47 PM
Help with getchar() please geraneum Programming 9 12-18-2008 04:42 AM
about getchar() captainstorm Programming 6 10-11-2003 04:14 AM
getchar() NSKL Programming 18 01-11-2003 10:19 AM
getchar() Winter Programming 6 05-11-2002 01:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration