Thread: Structure and Linked List User Input Question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Structure and Linked List User Input Question

    Okay my assignment is to create a program that accepts a user input for 5 values and stores them in a structure and then prints those 5 values back and allows for one more value to be inputted. I have created my structure but am having trouble with the use of pointers and how to get those five values stored. My C programming book is not of much help since it doesn't have an example for user input into a structure.


    My basic problem is the understanding of how do use pointers and store them in a structure.

    Thanks in advance for any help given.

    PHP Code:
    #include <stdio.h>
    struct oomba
        
    {
        
    int value;
        
    struct oomba *next;
        };


    int main()
    {

    struct oomba Val;
    oomba *first
    printf
    ("Please input the value 1");
    scanf("%d",Val.next);
    printf("\n\nPlease input the value 2");
    scanf("%d",Val.next);
    printf("Please input the value 3");
    scanf("%d",Val.next);
    printf("\n\nPlease input the value 4");
    scanf("%d",Val.next);
    printf("\n\nPlease input the value 5");
    scanf("%d",Val.next);
    first = &Val.next
    printf
    ("Your first five values are %d \n%d\n%d\n%d\n%d", *Val.next, *Val.next, *Val.next, *Val.next,*Val.next);



    return 
    0;


  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, so you're not looking at a Linkedlist correctly.

    Here's how you should look at it :

    Code:
    value 1
    next ----->  value 2
                  next ------>  value 3
                                 next ----> and so on
    Therefore, value2 would be in *(Val1.next).value (although you may want to do something like Val2 = *(Val1.next) and then Val2.value).

    Keep in mind, though, that what you're doing at the moment doesn't set up a linked list structure either. For the purpose of the question, I could see yourself simply modifying the structure to this :

    Code:
    struct oomba 
        { 
        int value[5]; 
        struct oomba *next; 
        };
    and being fine...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Revised Program No Luck

    Still working out the kinks. This gives me lots of errors, especially the storage of the values not reflecting the proper struct union type. I only wish my book was a little more helpful.

    Thanks,
    Kevin

    PS- Any additional advice would be greatly appreciated including a good faq or walkthrough of writing a program similar to this to let me know the basic format and what I am missing/ doing wrong. Thanks again for all the help

    PHP Code:
    #include <stdio.h>
    struct Oomba
        
    {
        
    int value[6];
        
    struct oomba *next;
        };


    int main()
    {
    int valueval1val2val3val4;


    oomba *first
    printf
    ("Please input the value 1");
    scanf("%d",value);
    printf("\n\nPlease input the value 2");
    scanf("%d",val1.next.value);
    printf("Please input the value 3");
    scanf("%d",val2.value);
    printf("\n\nPlease input the value 4");
    scanf("%d",Val3.value);
    printf("\n\nPlease input the value 5");
    scanf("%d",val4.value);
    Oomba *first
    first = &value;

    printf("Your first five values are %d \n%d\n%d\n%d\n%d"first->valueval1.next->valueval2.value->valueval3.value->valueval4.value->value);


    return 
    0;


  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, it looks like you're trying to declare first twice, and then you assign it to point to an integer (even though it's a pointer to a structure!). val1, val2 etc are ints but you're treating them as structures.

    [edit] And assuming you know that value is an int, you're missing an &:
    Code:
    scanf("%d",&value);
    [/edit]

    Perhaps you'd like to have a look at this program:
    Code:
    #include <stdio.h>
    
    struct numbers {
        int num[6];
    };
    
    void get_numbers(struct numbers *n);
    
    int main(void) {
        struct numbers n;
        int x;
    
        get_numbers(&n);
    
        for(x = 0; x < 6; x ++) {
            printf("%i\n", n.num[x]);
        }
    
        return 0;
    }
    
    void get_numbers(struct numbers *n) {
        int x;
    
        for(x = 0; x < 6; x ++) {
            printf("Enter number %i: ", x+1);
            scanf("%i", &n->num[x]);  /* no error checking */
        }
    }
    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.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try copying and pasting your code. If this is actually your code, then you have numerous problems. It helps to list the errors you're actually getting, so people don't have to waste time copying / compiling your broken code just to find out what's wrong.

    Code:
    int main() 
    { 
    int value, val1, val2, val3, val4; 
    
    
    oomba *first
    1 - This isn't C++, and you didn't typedef anything, so you can't just do "oomba", you have to do "struct oomba".
    2 - You don't have a ; at the end of this line.
    Code:
    printf("\n\nPlease input the value 5"); 
    scanf("%d",val4.value); 
    Oomba *first;
    Same thing goes here, except now you've got two variables called 'first' declared in the same scope. Not allowed.
    Code:
    first = &value;
    You're trying to assign the address of an integer to the pointer to a structure. You can't do that. Even if you could, it wouldn't be right.

    I think you're misunderstanding what the purpose of a linked list is. You have room in one structure for six integers, so you don't need a list. I suspect your list structure is designed wrong, or you're misunderstanding the purpose. I suspect you want one integer per structure, so that you hold one number per list element. Then you make a new node for each number.


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

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    new and improved but still not working

    Thanks for the sample program. I changed the structure to a different format (array) hoping that might alleviate some of my many problems. This program does compile but will not run.

    PHP Code:
    #include <stdio.h>
    struct Oomba
        
    {
        
    int *value;
        
    struct Oomba *next;
        };


    int main()
    {


    struct Oomba val[6];



    printf("Please input the value 1\n");
    scanf("%d",val[0].value);
    printf("\n\nPlease input the value 2\n");
    scanf("%d",val[1].value);
    printf("Please input the value 3\n");
    scanf("%d",val[2].value);
    printf("\n\nPlease input the value 4\n");
    scanf("%d",val[3].value);
    printf("\n\nPlease input the value 5\n");
    scanf("%d",val[4].value);


    printf("Your first five values are %d \n%d\n%d\n%d\n%d"val[0].valueval[1].valueval[2].valueval[3].valueval[4].value);


    return 
    0
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using an array? Do you want a linked list, or do you want an array? Why is there a pointer to an integer in your linked list now?


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

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Linked List/ Array

    I was hoping to gain some understanding of what i was doing and hoping by getting an array to work that would help me figure out my multiple problems with the linked list. The linked list is what I actually want to do but I am experimenting trying to figure it out.

    Thanks,
    Kevin

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write one program that enters them into an array.
    Write one program that enters them into an array in a structure.
    Write one program that enters them into an array of structures, each structure containing only one int.
    Write another program that enters them into a linked list.

    Remember, your linked list is acting like an array in that instead of just walking through the array, you move through the linked list via pointer manipulation. Your array node should only store one integer. The integer itself should just be a plain old int inside a structure, not a pointer to an int, unless you feel like calling malloc for each node to allocate that integer also.


    Quzah.
    Last edited by quzah; 10-04-2006 at 06:02 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Back on track

    This is what I got, I think I am getting the idea but now I am getting an error C2059 with Microsoft visual basic that says syntax error 'type' for the lines that scanf. I didnt write the display function yet, still trying to get this part to work.

    PHP Code:
    #include <stdio.h>
    struct Oomba
        
    {
        
    int value;
        
    struct Oomba *nextaddr;
        };

    void display(struct Oomba *);

    int main()
    {
    struct Oomba *first;
    struct Oomba v1,v2,v3,v4,v5,v6;

    first = &v1;
    v1.nextaddr = &v2;
    v2.nextaddr = &v3;
    v3.nextaddr = &v4;
    v4.nextaddr = &v5;
    v5.nextaddr = &v6;

    printf("Please input the value 1\n");
    scanf("%d",struct Oomba v1);
    printf("\n\nPlease input the value 2\n");
    scanf("%d",struct Oomba v2);
    printf("Please input the value 3\n");
    scanf("%d",struct Oomba v3);
    printf("\n\nPlease input the value 4\n");
    scanf("%d",struct Oomba v4);
    printf("\n\nPlease input the value 5\n");
    scanf("%d",struct Oomba v5);




    return 
    0;


  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf("Please input the value 1\n"); 
    scanf("%d",struct Oomba v1);
    Try:
    Code:
    printf("Please input the value 1\n"); 
    scanf( "%d", &v1.value );
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Thanks finally got that working

    That part is working now THANKS! Now I am trying to create the function to print out the list of the linked list and it compiles and runs up until the point it sends the input to the function. Here is the updated code with the display function.


    PHP Code:
    #include <stdio.h>
    struct Oomba
        
    {
        
    int value;
        
    struct Oomba *nextaddr;
        };

    void display(struct Oomba *);

    int main()
    {
    struct Oomba *first;
    struct Oomba v1,v2,v3,v4,v5,v6,v7,v8;

    first = &v1;
    v1.nextaddr = &v2;
    v2.nextaddr = &v3;
    v3.nextaddr = &v4;
    v4.nextaddr = &v5;
    v5.nextaddr = &v6;
    v6.nextaddr NULL;

    printf("Please input the value 1\n");
    scanf("%d",&v1.value);
    printf("\n\nPlease input the value 2\n");
    scanf("%d",&v2.value);
    printf("Please input the value 3\n");
    scanf("%d",&v3.value);
    printf("\n\nPlease input the value 4\n");
    scanf("%d",&v4.value);
    printf("\n\nPlease input the value 5\n");
    scanf("%d",&v5.value);

    display(first);

    return 
    0;

    }

    void display(struct Oomba *contents)
    {
    while (
    contents != NULL)
         {
         
    printf("%s",contents->value);
         
    contents contents->nextaddr;
         }
    return;


  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
    void display(struct Oomba *contents) 
    { 
    while (contents != NULL) 
         { 
         printf("%s",contents->value); 
         contents = contents->nextaddr; 
         } 
    return; 
    }
    I don't think you want %s in there. Probably more like %d.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Got it working

    So now my function and structure work with the input of the 5 values. The assignment me asks us to have the user input 5 values then print the results and then input one more value. I am trying to figure out how to get that to work. Should I write another functiion?

    PHP Code:
    #include <stdio.h>
    struct Oomba
        
    {
        
    int value;
        
    struct Oomba *nextaddr;
        };

    void display(struct Oomba *);

    int main()
    {
    struct Oomba *first;
    struct Oomba v1,v2,v3,v4,v5,v6,v7,v8;

    first = &v1;
    v1.nextaddr = &v2;
    v2.nextaddr = &v3;
    v3.nextaddr = &v4;
    v4.nextaddr = &v5;
    v5.nextaddr = &v6;
    v6.nextaddr NULL;



    printf("Please input the value 1\n");
    scanf("%d",&v1.value);
    printf("\n\nPlease input the value 2\n");
    scanf("%d",&v2.value);
    printf("Please input the value 3\n");
    scanf("%d",&v3.value);
    printf("\n\nPlease input the value 4\n");
    scanf("%d",&v4.value);
    printf("\n\nPlease input the value 5\n");
    scanf("%d",&v5.value);

    display(first);

    return 
    0;

    }

    void display(struct Oomba *contents)
    {
    while (
    contents != NULL)
         {
         
    printf("\n%d",contents->value);
         
    contents contents->nextaddr;
         }
    return;


  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    This is my attempt at inputting an additional var.

    This is my first try at getting the 6th value inputted. Compiles but crashes after the first 5 values are inputted and displayed.


    PHP Code:

    #include <stdio.h>
    struct Oomba
        
    {
        
    int value;
        
    int value2;
        
    struct Oomba *nextaddr;
        };

    void display(struct Oomba *);
    void display2(struct Oomba *);
    int main()
    {
    struct Oomba *first;
    struct Oomba v1,v2,v3,v4,v5,v6,v7,v8;

    first = &v1;
    v1.nextaddr = &v2;
    v2.nextaddr = &v3;
    v3.nextaddr = &v4;
    v4.nextaddr = &v5;
    v6.nextaddr NULL;



    printf("Please input the value 1\n");
    scanf("%d",&v1.value);
    printf("\n\nPlease input the value 2\n");
    scanf("%d",&v2.value);
    printf("Please input the value 3\n");
    scanf("%d",&v3.value);
    printf("\n\nPlease input the value 4\n");
    scanf("%d",&v4.value);
    printf("\n\nPlease input the value 5\n");
    scanf("%d",&v5.value);

    display(first);

    printf("\n\nPlease input the value 6\n");
    scanf("%d",&v1.value2);
    display2(first);

    return 
    0;

    }

    void display(struct Oomba *contents)
    {
    while (
    contents != NULL)
         {
         
    printf("\n%d",contents->value);
         
    contents contents->nextaddr;
         }
    return;
    }

    void display2(struct Oomba *contents)
    {
    while (
    contents != NULL)
         {
         
    printf("\n%d",contents->value);
         
    contents contents->nextaddr;
         
    printf("\n%d"contents ->value2);
         }
    return;


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM