Thread: trouble with fgets() !

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    22

    trouble with fgets() !

    here is my problem! i'm trying to intput a string using fget() method. The problem is it doesn't allow me to do it. Here is what i did:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int coursenumber;
    	char coursetitle[50];
    	
    	printf("Enter Your Course Number:\n");
    	scanf("%i", &coursenumber);
    	printf("%i\n", coursenumber);
    	
    	printf("Enter Your Course Titlte:\n");
    	fgets(coursetitle, 50, stdin);
        printf("%s", coursetitle);
    	
    	system("PAUSE");
    	return 0;
    	
    }
    i was able to input the course number and print it out, but after i hit enter, it skip the fget() and never give me a chance to input a string, and it end up "Press any key to continue". I have checked exactly what to do when using fget() method to input a string, and i did the same as tutorial in this website.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's not fgets() fault. It is the fact that you are mixing scanf() with fgets(). scanf() leaves anything that wasn't part of the actual data in the input buffer. So when you enter coursenumber 123 and hit enter, the 123 is eaten by scanf, but the newline representing the enter is left behind.

    Then fgets reads everything up until the next newline. Well, a newline is the very next character in the input buffer, so it's simply grabbing that and saying "Ok, done".

    You need to either consistenly use fgets to read all the data, or add a bit of code to "eat" the newline left behind. A call to getchar() will work for this purpose.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Is your intent to read in the input as hex? You should use &#37;d specifier if not.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quick summary:

    '&#37;i' : Scan an integer as a signed number. Similar to '%d', but interprets the number as hexadecimal when preceded by "0x" and octal when preceded by "0".

    For example, the string "031" would be read as 31 using '%d', and 25 using '%i'.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Hint: You can use "int atoi(char*)" to conver a string (obtained by fgets) to an int. So you avoid mixing scanf and fgets. You can use "int strtoi(char*, char**, int)" like strtoi(string, NULL, 16) to convert the string to a hex

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Hint: You can use "int atoi(char*)" to conver a string (obtained by fgets) to an int. So you avoid mixing scanf and fgets. You can use "int strtoi(char*, char**, int)" like strtoi(string, NULL, 16) to convert the string to a hex
    That would be strtol, as strtoi is not in the standard C set of functions.

    If you give the last parameter (base) as 0, it will follow the same rules as %i, that is: strings that start with 1..9 are decimal, strings that start with 0 are octal, and strings that start with 0x are treated as hex. Give base 10 to read decimal numbers only.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I once worked with a database that stored numbers in a base 256 format. Random. But nifty, no less.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    I once worked with a database that stored numbers in a base 256 format. Random. But nifty, no less.
    Ehm, isn't that how ALL computers store numbers? 1st byte = 0..255, 2nd byte = 0..255 * 256, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yep. I never said otherwise. I am merely stating the obvious that strtol() has some limitations as to what base you can use. It was for a bank with arbitary length numbers. Though like most banks, you aren't going to be getting numbers truly beyond 64-bit computation...

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Random indeed as I cant even imagine a number in radix 256, since there are so many digits to think about.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I didn't design it. Such types of systems were invented waaaaaayyyyyyyy before my time when memory was an expensive luxury.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    all right! thank everybody very much! actually i'm just a beginner for this! reading u guys discussion, i'm really get into it even i dont really understand it. Probably later, i will understand well what u guys are discussion. Anyway, very helpful, i appreciate that. I fixed the problem by what matsp said, use getchar() to read the newline. Pretty simple and easy to fix. ^_^

  13. #13
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by davewang View Post
    all right! thank everybody very much! actually i'm just a beginner for this! reading u guys discussion, i'm really get into it even i dont really understand it. Probably later, i will understand well what u guys are discussion. Anyway, very helpful, i appreciate that. I fixed the problem by what matsp said, use getchar() to read the newline. Pretty simple and easy to fix. ^_^
    I had that same problem a while ago, you can do that or you can use fgets together with sscanf, that would do the trick also.

    fgets(whatever,sizeof(whatever),stdin);
    sscanf(whatever,"%d",&num);
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Which is surely a prefered way to do it since it eliminates the mixing of scanf() and fgets().

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. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  3. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM