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;
}
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].value, val[1].value, val[2].value, val[3].value, val[4].value);
return 0;
}
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;
}
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;
}