I'm stuck

This is a discussion on I'm stuck within the C Programming forums, part of the General Programming Boards category; I have a program to write and I have no idea on what to do. I need to be able ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    I'm stuck

    I have a program to write and I have no idea on what to do. I need to be able to enter in a list of names like this:
    Doe, John
    White, Tim
    Brown, Dan
    etc...

    then it has to come out like this:
    John Doe
    Tim White
    Dan Brown

    I have absolutely no idea how to do it.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to read from the keyboard? Start there.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    ill give u a little hint

    create two arrays,
    one array has the first input
    put the last entry of the array into the first entry of the second array
    out put the second array

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    char name[80];

    printf("enter name: ");
    fgets(name, sizeof(name), stdin);

    Think of what you got to split the name - a space and a comma. Use them.

    Check into strstr.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    so...

    strstr means string string right? I was thinking I'd have to use a swap in there...

    So I have to have 2 arrays. I knew that...
    But that is not my problem, don't get me wrong, I appreciate all the help so far. but I don't understand why I'd need to pull the name from a file... (fgets)
    Um...
    lets see... where else...
    ah
    I don't know what you mean about using the space and the comma.. Is that just using
    scanf("%s", &variable);
    or are you getting at something else?

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>to pull the name from a file... (fgets)
    fgets don't take data from a file, it take a data from a stream and a stream can be a file, or even stdin (standard input).

    >>scanf("%s", &variable);
    you'll get some erros using this after sometimes you call it, also you don't need the '&' operator here if variable it's really a string.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fgets() gets from the keyboard by specifying the file as stdin. This is better than using scanf() or gets().

    To determine the location of a comma in a string, use strchr() (as opposed to strstr() ).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    We aren't supposed to use the input from an outside file. We "learned" about that yesterday. My teacher isn't really good at teaching. He knows 'C' way too well and feels that every one should be fluent in it. I think I could do it using scanf statements, but that would take a longer time and isn't what the program is being written for.
    I'm beyond stuck... I get brainstorms but they all just fizzle into a little static shock. I'm gonna fail this stupid course...

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This thread is getting far too long with nothing at all being done.
    Code:
    #include <stdio.h>
    int main( void )
    {
        char buf[BUFSIZ] = {0};
    
        printf("Enter a string:\n");
        fgets( buf, BUFSIZ, stdin );
    
        printf( buf );
    
        return 0;
    }
    Like everyone has been saying, fgets is not only used for files. There, you do the rest.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    I'm gonna go try that now. Thanks. I'll let you know if this works for me. BTW, the program we use for this is turbo C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 05:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 09:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21