printing int in structure...weird

This is a discussion on printing int in structure...weird within the C Programming forums, part of the General Programming Boards category; I have a block of code that takes input from stdin and stores it in a variable in a structure. ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    Toronto
    Posts
    11

    printing int in structure...weird

    I have a block of code that takes input from stdin and stores it in a variable in a structure. then i go to print the contents of the structure but for some reason it keeps printing the last this entered from stdin.

    after the user inputs their name and age, it goes back to the main menu where i choose to print names, option 3, which should print the name and age but it always prints 3 the last thing entered via stdin as opposed to the age previously entered

    anyone know what the problem could be?

    Thanks, ALain

    ps. the program is nowhere neer complete for the task it needs to accomplish...ie. records placed in a sorted linked list..... so i appologize if bits and pieces confuse you the functions are not complete i just want to understand why i'm having this issue with stdin.

    i'm compiling using gcc -ansi -Wall in a linux terminal.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <strings.h>
    #include <ctype.h>
    
    #define MAX_LEN 250
    
    struct node 
    {
        char *name;
        int  age;
        struct node *nextName;
        struct node *nextAge;
    };
    
    typedef struct node NODE;
    
    int drain_stdin()
    {
        int c;
    
        while((c = getchar()) != '\n' && c != EOF );
    
        return c;
    }
     
    void addStruct(NODE *info)
    {
    
        if (drain_stdin() != EOF )
        {
            printf("Enter name: ");
            fgets(info->name, MAX_LEN + 1, stdin);
    
            printf("Enter age: ");
            scanf("&#37;d", &info->age);
        }
        else
        {
            printf("An Error Has Occured\n");
        }
    
    }
    
    void removeStruct(NODE *info)
    {
    
        if (drain_stdin() != EOF )
        {
            printf("Enter Name Of Desired Record To Be Removed");
            fgets(info->name, MAX_LEN + 1, stdin);
        }
        else
        {
            printf("An Error Has Occured\n");
        }
    }
    
    void printName(NODE *info)
    {
        printf("%s %d\n", info->name, info->age);
    }
    
    void printAge(NODE *info)
    {
        printf("%s %d\n", info->name, info->age);
    }
    
    /*
    void exit(NODE *info)
    {
        free(info->name)
        free(info);
    }
    */
    
    int main() 
    {
        int choice;
    
        NODE *info;
        info = malloc(sizeof(NODE));
        info->name = malloc(sizeof(char)*MAX_LEN);
    
        do
        {
            printf("1. Add structure\n");
            printf("2. Remove structure\n");
            printf("3. Print names\n");    
            printf("4. Print ages\n");
            printf("5. Exit\n");
            scanf("%d", &choice);
    
            if(choice == 1)
            {
                addStruct(&info);
            }
            else if(choice == 2)
            {
                removeStruct(&info);
            }
    
            else if(choice == 3)
            {
                printName(&info);
            }
            else if(choice == 4)
            {
                printAge(&info);
            }/*
            else if(choice == 5)
            {
                exit(&info);
            }*/      
            else
            {
                return 0;
            }
            
        }while(choice != 5);    
    
        return 0;
    }
    Last edited by S15_88; 03-19-2008 at 04:52 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see why that should be.

    Can you print "info->age" just after you read it in? Not that there is a reason why it should change, of course.

    --
    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
    Join Date
    Mar 2008
    Location
    Toronto
    Posts
    11
    sorry i appologize i found my own error.

    THREAD SOLVED

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about posting what it was, just to satisfy our curiosity? And you never know, it may help someone else too...

    --
    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.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well it looks as if you are only creating one node object. So each time you enter new data you are simply overwriting what was in the object before.

    Also, I just made a couple more comments about it here:
    http://ubuntuforums.org/showthread.php?t=729268

    cheers.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    #include <strings.h>
    What's that? Seriously, that's a non-standard header file, and since you don't seem to be using anything from it, you probably shouldn't include it.

    sizeof(char) is always 1.

    Code:
        info->name = malloc(sizeof(char)*MAX_LEN);
    /* ... */
            fgets(info->name, MAX_LEN + 1, stdin);
    Looks like you might be going one too far there. Don't add 1 to the fgets() size.

    Code:
        NODE *info;
    /* ... */
                addStruct(&info);
    /* ... */
    void addStruct(NODE *info)
    {
    I think you have one too many levels of indirection there. Passing just "info" is more likely to be what you want. This applies to the other functions as well.

    Nice drain_stdin() function, BTW.
    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. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  3. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

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