Thread: Using fgets?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    99

    Using fgets?

    Hey,
    i ahve asked you some days before how to use scanf to add string
    but i think that if you use fgets you can take strings faster
    the problem is i don't know how to get a string
    for example i got this:
    Code:
    #include <stdio.h>
    
    main()
    {
    printf("Hello, what is your name?");
    fgets(name, sizeof name, stdin);
    printf("Your name is %s", name);
    }
    this code isn't working and i don't know why?
    please help me using fgets
    thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, it could be because you haven't declared name anywhere. Did you read your compiler errors?
    Code:
    itsme@dreams:~/C$ cat noname.c
    #include <stdio.h>
    
    main()
    {
    printf("Hello, what is your name?");
    fgets(name, sizeof name, stdin);
    printf("Your name is %s", name);
    }
    itsme@dreams:~/C$ gcc -Wall noname.c -o noname
    noname.c:4: warning: return-type defaults to `int'
    noname.c: In function `main':
    noname.c:6: `name' undeclared (first use in this function)
    noname.c:6: (Each undeclared identifier is reported only once
    noname.c:6: for each function it appears in.)
    noname.c:8: warning: control reaches end of non-void function
    itsme@dreams:~/C$
    Check out the lines I bolded.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cogeek
    the problem is i don't know how to get a string

    please help me using fgets
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    because you have not reserved a place in memory to store that string. You know how to declare a string right? if so , then declare name as a string first . If not , then read about strings first.

    You've also declared function main as 'main()' without a return value. You should explicitly declare 'main' as an int (int main) and let it return a value to indicate successful execution (typically 0), so your program would look like this :

    Code:
    #include <stdio.h>
    
    int main()
    {
    /* code here */
    
    return 0;
    }
    btw , your compiler shows errors for a reason..
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 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