GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Sep-2003, 00:44
tmike tmike is offline
New Member
 
Join Date: Sep 2003
Posts: 3
tmike is an unknown quantity at this point

a C input question..


Hi, guys...I have a question about keyboard input in C

let's say I have a program which allows user to type in command
commands are in the form:
command parameter1 parameter2...etc

since commands have different # of parameters..I'll need to use fgets or gets to get the whole command string and parse it...

also, I'll allow user to continue inputing command except when they type in "quit"

this is how I do it..
CPP / C++ / C Code:
char command[100], *com;

for(;; )
{
       com = fgets(command, 50, stdin);
       // this line just get the whole command as a string

      if(strcasecmp(command, "quit") == 0)
			break;

      execute(com);
      // this is a function which parse the whole command string
         and  execute approapriate functions...

}

but here comes a problem...
if a user didn't type anything, but just hit ENTER....it'll have a segmentation fault, becuz the fgets will have some problem with nothing entered....I thought that the fgets will return NULL..so I can use that condition to detect this kind of situation and just loop around again.....but it doesn't happen that way...

I was wondering if there's a way I can do to make my program keeps waiting for user command, even if the user decided to press a whole lot of ENTERs, it'll just loop around..

tks....
  #2  
Old 19-Sep-2003, 01:53
tmike tmike is offline
New Member
 
Join Date: Sep 2003
Posts: 3
tmike is an unknown quantity at this point
ha....I figured it out...
sorry about the post.....
  #3  
Old 19-Sep-2003, 03:39
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
No worries! You wanna say how you fixed it?

GF
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
a C input question tmike C Programming Language 1 16-Sep-2003 03:31
a noobish compiler question Charunks C++ Forum 5 03-Sep-2003 03:18
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 03:54
'Related articles' php /mysql question JdS MySQL / PHP Forum 4 06-Sep-2002 11:17
PHP Sessions question JdS MySQL / PHP Forum 2 16-Jul-2002 05:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.