Thread: Adding a second set of values to a linked list

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Adding a second set of values to a linked list

    I have a second assignment that I cant seem to get right
    Create a structure that has one variable called value and one pointer to the list (making it a linked list). Prompt for 5 values from the keyboard as input and store them in the linked list. Print out the current contents of the list. Allow the user to add one more value to the linked list, and print the contents of the list again.

    The code I have so far is

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXCHARS 5
    
    
    typedef struct NUMBERS
    {
           int value;
           struct NUMBERS *next;
           } node_t;
    
    int main()
    {
        int i, value;
       
        char name[MAXCHARS];
        
        for (i=0; i < MAXCHARS; i++) 
          printf("Enter five numbers, one per line.\n");
          scanf("%d", &value[i]);
        
        printf("Your five numbers are:\n");
        
        for (i = 0; i < MAXCHARS; i++)
          printf(" %d\n", value[i];
        
        free(value);
        scanf("%d", &i); 
        return 0;
        }
    I am getting errors on a couple of lines and I can't figure out what I am doing wrong

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Value is not an array, but you are trying to treat it like one.

    Code:
        for (i=0; i < MAXCHARS; i++) 
          printf("Enter five numbers, one per line.\n");
          scanf("&#37;d", &value[i]);
    C, unlike Python, does not group something into a block merely be indentation. You must use brackets ({ and }).

    Name is not a string, it's an array of 5 chars, and furthermore, unused.
    You cannot free a non-pointer not allocated with malloc!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    What can I do to correct my wrong doings?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you need to get a better grasp of the basics.
    This are fundamental problems, not logic errors.
    Use books/tutorials and learn from them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    I have been at this problem for an hour or two and have yet to crack it. Can any one please give me some insight into what I am doing wrong and any possible help.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You should try something easier: store the 5 values in a array for now. Once it works, replace the array by a linked list.

    Try:

    1/ declare an array of 5 integers
    2/ read 5 integers from the input and store them in the array using a loop.
    3/ output the array.

    You're not far from doing that with your code.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, I would second root4. Do it step-by-step and make sure you learn the proper syntax and ways of doing it in C before moving on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. Adding to linked list from external file
    By cghv in forum C Programming
    Replies: 1
    Last Post: 03-09-2005, 02:05 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM