C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-03-2006, 04:38 PM   #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;

kevndale79 is offline   Reply With Quote
Old 10-03-2006, 05:22 PM   #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
Happy_Reaper is offline   Reply With Quote
Old 10-04-2006, 04:20 PM   #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;

kevndale79 is offline   Reply With Quote
Old 10-04-2006, 04:35 PM   #4
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Old 10-04-2006, 04:43 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 10-04-2006, 04:50 PM   #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
}
kevndale79 is offline   Reply With Quote
Old 10-04-2006, 05:25 PM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 10-04-2006, 05:48 PM   #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
kevndale79 is offline   Reply With Quote
Old 10-04-2006, 05:55 PM   #9
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?

Last edited by quzah; 10-04-2006 at 06:02 PM.
quzah is offline   Reply With Quote
Old 10-04-2006, 06:00 PM   #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;

kevndale79 is offline   Reply With Quote
Old 10-04-2006, 06:05 PM   #11
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,640
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 10-04-2006, 06:32 PM   #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;

kevndale79 is offline   Reply With Quote
Old 10-04-2006, 06:38 PM   #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
Happy_Reaper is offline   Reply With Quote
Old 10-05-2006, 06:44 AM   #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;

kevndale79 is offline   Reply With Quote
Old 10-05-2006, 06:58 AM   #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;

kevndale79 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Following CTools EstateMatt C Programming 5 06-26-2008 10:10 AM
help! Placement of nodes in a Linked List lostmyshadow C Programming 6 12-17-2007 01:21 PM
Problem with linked list ADT and incomplete structure prawntoast C Programming 1 04-30-2005 01:29 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
singly linked list clarinetster C Programming 2 08-26-2001 10:21 PM


All times are GMT -6. The time now is 09:45 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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