Search code examples
clinked-listuser-input

Storing user input into linked list - C


I have an assignment for C beginner class: I'm supposed to create a structure that has one variable (named value) and one pointer to the list. I'm supposed to prompt the user for 5 values as input and store them in the linked list. Then print out the list. I can't figure out how to store input into the list. I did manage to write a program in which I initialize the 5 values. But I don't know how to accept input and store it into the list.

Here's the working program where I initialize the values:

#include <stdio.h>

struct list
{
    double value;
    struct list *nextVal;
};

int main()
{
    struct list v1 = {10};
    struct list v2 = {17.97};
    struct list v3 = {166};
    struct list v4 = {2};
    struct list v5 = {387.55};
    struct list *first;

    first = &v1;
    v1.nextVal = &v2;
    v2.nextVal = &v3;
    v3.nextVal = &v4;
    v4.nextVal = &v5;
    v5.nextVal = NULL;

    printf("All the values are: %.2f, %.2f, %.2f, %.2f, %.2f.",
    first->value,v1.nextVal->value,v2.nextVal->value,
    v3.nextVal->value,v4.nextVal->value);

    return 0;
}

Here's a program where I tried getting user input and storing that into the list. (I only have 2 values instead of 5, cuz it's easier to work with that when trying to make it work.) When I compile this program, I get no errors. When I run it, I am properly prompted for the two input values; however, when the output that the program prints just writes 0 for both values. My assumption is that I'm storing the value into the list wrong - because I don't know how it's actually done. (Can't find any info in my textbook or online.) Last night someone commented on a different question I had to try using breakpoint to find out exactly where the problem is - I've been trying to figure it out since then but don't know how to do that yet. (I'm assuming it's something very simple - but everywhere I looked online, people explain it in a way that seems like I'm supposed to know what they're talking about - which I don't - And honestly, even if I could figure it out, I still wouldn't know what to do with that information, since I just can't find any information on how to store user input into a linked list.) Anyways, so here's my trial program that complies and runs but gives me 0 for both final values:

#include <stdio.h>

struct list
{
    double value;
    struct list *nextVal;
};

int main()
{
    double val1,val2;

    printf("Please enter a number: ");
    scanf("%f",&val1);
    printf("Please enter a number: ");
    scanf("%f",&val2);

    struct list v1 = {val1};
    struct list v2 = {val2};
    struct list *first;

    first = &v1;
    v1.nextVal = &v2;
    v2.nextVal = NULL;
    printf("The two values entered are %.2f and %.2f.",
    first->value,v1.nextVal->value);

    return 0;
}

Thank you in advance! And I'm reaaaally a beginner - so even if you suggest something super easy - I might have no clue what it is..so please explain! Thank you!


Solution

  • scanf("%f",&val1);
    

    %f requires a float variable but the given variable is a double. Use %lf for double.

    scanf("%lf",&val1);
    

    Your compiler should have given you a warning for that and you should always heed those and resolve them:

    warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
     scanf("%f",&val1);
    

    A few extra words of advice though they are not directly addressing your question:

    • Always check the function return values. Specifically, check scanf to ensure that the expected input was parsed. The code as it is will fail if the user enters something unexpected.
    • Break up your code into functions. Specifically, make a seperate insert function to add items to the linked list.
    • Use loops where you need to repeat code. Such as for the scanf calls. That way it can be easily extended when you want to take more input.