Thread: problems with fgets

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    problems with fgets

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    struct ymd {
    int year, month, day;
    char name;
    } birth;
    
    printf("What's your name? ");
    fgets(&birth.name, sizeof(birth.name), stdin);
    getchar();
    printf("\nWhat's your year of birth?(xxxx): ");
    fscanf(stdin, " %d", &birth.year);
    
    printf("\nWhat is your birth month?(xx): ");
    fscanf(stdin, " %d", &birth.month);
    
    printf("\nWhat is your birth day?(xx): ");
    fscanf(stdin, "%d", &birth.day);
    
    printf("%s %d %d %d\n", &birth.name, &birth.year, &birth.month, &birth.day);
    }
    I run the program and with the getchar function it takes the name in but then skips over the rest and exits back to the prompt. If I don't use getchar it skips over the name and takes the rest of the input. Should I use an alternative or is this a fixable problem?

    And I want to create a data base for birthdays and prompt the user for a name and depending on the name, return the correct info. Or an error if the name/info does not exist. I'm guessing I should use a linked list?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    INDENT!

    Quote Originally Posted by Annonymous View Post
    Code:
    int main(void) {
    struct ymd {
    int year, month, day;
    char name;
    } birth;
    
    printf("What's your name? ");
    fgets(&birth.name, sizeof(birth.name), stdin);
    I think you know that the sizeof(birth.name) here is 1, because birth.name is a single char. That's not what you want.

    If you then typed in a string like "Master Shake", that would leave enough bytes left in the stdin buffer to fulfill the rest of the stuff, which is why it seems to skip.

    And I want to create a data base for birthdays and prompt the user for a name and depending on the name, return the correct info. Or an error if the name/info does not exist. I'm guessing I should use a linked list?
    Sounds good. You may also want to come up with a simple serialization scheme so you can write/read these to disk and don't have to keep entering stuff every time you test. A good idea with that would be to use a serialization scheme that can be read using the exact same code you use for console input, eg:

    Code:
    Master Shake
    1971
    4
    20
    
    Frylock
    1966
    6
    6
    
    Meatwad
    1973
    2
    29
    Would be a text file you could read into a list with a loop. The blank line is a double '\n', which you can use as a delimiter. In order to use the same code for both file and console input, you'd have a function like this:

    Code:
    struct llnode *addnode (FILE *str, int ask) {
          struct llnode *rv = malloc(sizeof(struct llnode));
          
          if (ask) printf("What's your name? ");
          fgets(&rv.name, sizeof(rv.name), str);
          [..etc]
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Nvm, I was taking in the name as an int and not a string. /facepalm
    Code:
    #include <stdio.h>..
    #include <string.h>
    
    int main() {
    struct ymd {
    .... ..int year, month, day;
    .... ..char name[50];
    .... ..} birth, birth2, *p;
    p = &birth2;
    printf("What's your name? ");
    fgets(p->name, sizeof(p->name), stdin);
    
    printf("\nWhat's your year of birth?(xxxx): ");
    fscanf(stdin, " %d", &birth.year);
    
    printf("\nWhat is your birth month?(xx): ");
    fscanf(stdin, " %d", &birth.month);
    
    printf("\nWhat is your birth day?(xx): ");
    fscanf(stdin, "%d", &birth.day);
    
    printf("%s %d %d %d\n", p->name, birth.year, birth.month, birth.day);..
    return 0;
    }

  4. #4
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    *single char

  5. #5
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    I have the base of the program working.
    Code:
    #include <stdio.h>..
    #include <string.h>
    #include <stdlib.h>
    
    int main() {
    FILE *fp;
    struct ymd {
    int year, month, day;
    char name[50];
    } birth, birth2, *p;
    p = &birth2;
    printf("What's your name? ");
    fgets(p->name, sizeof(p->name), stdin);
    
    printf("\nWhat's your year of birth?(xxxx): ");
    fscanf(stdin, " %d", &birth.year);
    
    printf("\nWhat is your birth month?(xx): ");
    fscanf(stdin, " %d", &birth.month);
    
    printf("\nWhat is your birth day?(xx): ");
    fscanf(stdin, "%d", &birth.day);
    if((fp = fopen("bday_dbase.txt", "a")) == NULL) { printf("Error opening file.");..
    exit(1);..
    } else {
    fprintf(fp, "%s %d %d %d\n", p->name, birth.year, birth.month, birth.day);..
    }
    return 0;
    }
    I have never used a linked list before so I don't exactly know how to tie it into what I have. And sorry about the INDENTATION but I don't have a computer and I'm working on getting a laptop. A $300 HP on lay away.

  6. #6
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    P.S., I'm using an iPhone 4 to write, compile, and run.

  7. #7
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    I'm guessing I take the input from file and add it to the list. Make options for enter new entry, or search for old one, etc;. And just add node as I need, delete as need too. Am I wrong?

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    UB if user puts a non integer or a value > INT_MAX or < INT_MIN.
    name will contains a '\n' mostly.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Read through Lesson 15: Linked List. That should answer most of your questions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with fgets
    By marcoesteves in forum C Programming
    Replies: 11
    Last Post: 01-20-2011, 08:13 AM
  2. Problems with fgets not accepting input from stdin
    By k2712 in forum C Programming
    Replies: 7
    Last Post: 08-25-2007, 11:33 PM
  3. fgets
    By CaN Opner in forum C Programming
    Replies: 8
    Last Post: 01-14-2003, 11:58 PM
  4. fgets
    By New_to_C in forum C Programming
    Replies: 18
    Last Post: 08-19-2002, 08:33 AM
  5. xor (was fgets)
    By Brian in forum C Programming
    Replies: 3
    Last Post: 08-18-2002, 08:13 PM