Thread: fgets, sscanf question.....

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    fgets, sscanf question.....

    The code below is kinda hard to read, it's a portion of a program I'm trying to make for ANSI DOS. I used fgets and sscanf (only learned about sscanf yesterday - but I see already it rocks), and the program works perfect so far. But I'm curious as to why, when I first tried it, I delcared a char buf[2], figuring that meant that buf[2] = '/0'. Then I had origionally had it where (fgets(buf,1,stdin); - however, when I did, when that part of the program loaded, it would immediatly say "You did not enter a number" - I mean as soon as it came up on the screen, you never had a chance to type anything. Changing it to 2 works just fine, and I know in most examples it's char buf[256], but in this case I'm only receiving one letter, for a menu selection, so I really don't need an array that large. Like I said, it's fixed, I just changed it to 2, and now it works great, but I'd like to know why when you just have fgets read in 1 instead of 2 it does that.

    Thanks again,

    ash

    Code:
    void intro() {
    
    	cls();
    
    	printf("\x1b[31m");
    
    	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n");
    printf("                     Welcome to Smith Sayings - Freeware Edition\n");
    	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    	printf("\x1b[34m");
    	printf("                            Press <ENTER> to continue...\n");
    
    	getchar(); 
    	printf("\x1b[0m");
    return;
    
    }
    		
    (Sorry this below is so hard to read, it was too long to retype,
    and I copied it from DevC to the clipboard, to here)
    
    void menu() {
    	int choice;
    	char buf[2]; <--if you look here, it's 2, then look below
    	cls(); 
    (skip down reading to right below all the "***********'s")
    (that's where the problem is, this stuff in the middle is the main..)
    (..menu that I made, which looks horrible posted in this tiny..)
    (..window)
    
    	printf("\x1b[34m");
    	
    	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    	printf("                         ***********Main Menu**********                        ");
    	printf("                          *                            *                        ");
    	printf("                          *\x1b[31m         1)\x1b[34mRun Story        *                        ");
    	printf("                          *                            *                        ");
    	printf("                          *                            *                        ");
    	printf("                          *                            *                        ");
    	printf("                          *                            *                        ");
    	printf("                          *\x1b[31m         2)\x1b[34mAbout            *                        ");
    	printf("                          *                            *                        ");
    	printf("                          *                            *                        ");
    	printf("                          *                            *                        ");
    	printf("                          *                            *                        ");
            printf("                          *\x1b[31m         3)\x1b[34mExit             *                        ");
    	printf("                          *                            *                        ");
            printf("                          ******************************                        ");
    
    	printf("\n\n\n\n");
    	printf("                                 Choice:");		
    gets(buf,2,stdin); //<----why won't a 1 instead of 2 work here?
    	if (sscanf(buf,"%d",&choice) != 1) {
    		printf("Please enter a valid selection\n");
    		printf("Press <ENTER>\n");
    		getchar();
    		menu();
    		} 
    printf("blah");//if got this far, must be number.
    return;

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    oh, I see it came out looking much better once I posted it....

  3. #3
    Registered User freespace's Avatar
    Join Date
    Nov 2005
    Posts
    7
    fgets reads 1 *less* than the number of characters you specified. So if you give it 1, it reads 0 characters. Thats why you need the 2.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    wow, that makes sense....thanks!

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Thanks again for the input, it not works great, but 1 more question, now if the user enters 2 letters, instead of a number, it says "Please enter a valid selection" like it should. However, if they only enter 1 letter and hit enter, it just clears the screen and makes whatever they entered disappear, and repeats the menu, which isn't a big deal, but I'm wondering why...? It's like, if you only enter 1 letter, it goes, cls, menu(), instead of cls, "please enter blah blah blah" like it should....

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Also, I just noticed, if you enter a really large number, it prints "Invalid Number" over and over, which is what I put in the DEFAULT part of the switch statement that I added under it.
    Last edited by Ash1981; 01-04-2006 at 03:34 AM. Reason: mistake

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    actually, I just replaced my switch statement with if conditional statements, and things are looking much better, guess I fixed that one on my own

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    One thing I'd still like to know however, is how to "trap" the cursor...for instance, make it so that when I'm only expecting like 1 letter as input from stdin, they can't keep typing past 1 letter...for instance: Choice:_ and where the underscore is, if they try to type more than that, it can't look like this: Choice:_fjdkfjdkfdjkdfjk, and continue on past and down to the next line....a way so that it(cursor) can only reside in that one blank....ya know?

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    C has no direct support for such a thing (cursor manipulation).

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > a way so that it(cursor) can only reside in that one blank....ya know?
    Already answered - http://cboard.cprogramming.com/showthread.php?t=74053

    Or read each character yourself (not using fgets to get a whole line for yourself) and use the ANSI escape sequence for moving the cursor.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    When you say, already awnsered, well I went back and read what you had written there, but I'm not quite sure I know what you mean. Can you please elaborate?

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    I mean, what function can do that? Thanks for the help.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I mean, what function can do that?
    Manipulate the mouse, you mean? Try searching google, or this board for that matter.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Fgets + sscanf
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-15-2004, 08:53 PM
  4. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  5. fgets problem (i think)
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 11:19 AM