problems with structures and linked lists

This is a discussion on problems with structures and linked lists within the C Programming forums, part of the General Programming Boards category; Im trying to create a structure that has a pointer to a list and allow someone to enter 5 grades. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    21

    problems with structures and linked lists

    Im trying to create a structure that has a pointer to a list and allow someone to enter 5 grades. Display that and then enter one more followed by another reprint. Just keep getting an error saying "expecting struct Grade"

    any help would be great i have been stuck for a day and a half on this.

    Code:
    #include <stdio.h>
    
    struct Grade
    {
    	int value;
    	struct Grade *nextaddr;
    };
    
    int main () 
    {
    	struct Grade number1, number2, number3, number4, number5, *first;
    	void display (struct Grade *);
    	int num1, num2, num3, num4, num5, num6;
    
    	printf("Enter first grade: ");
    	scanf("%d", &num1);
    	printf("\nEnter second grade: ");
    	scanf("%d", &num2);
    	printf("\nEnter third grade: ");
    	scanf("%d", &num3);
    	printf("\nEnter fourth grade: ");
    	scanf("%d", &num4);
    	printf("\nEnter fifth grade: ");
    	scanf("%d", &num5);
    
    	number1.value = num1;
    	number2.value = num2;
    	number3.value= num3;
    	number4.value = num4;
    	number5.value = num5;
    
    
    	first = &number1;
    	number1.nextaddr = &number2;
    	number2.nextaddr = &number3;
    	number3.nextaddr = &number4;
    	number4.nextaddr = &number5;
    	number5.nextaddr = NULL;
    	
    	printf("\nTHE GRADES ENTERED ARE:\n");
    	
    	display(first);	
    	
    	struct Grade number1, number2, number3, number4, number5, number6, *first;
    	
    	printf("\nEnter on additional grade: ");
    	scanf("%d", &num6);
    
    	
    	
    	number1.value = num1;
    	number2.value = num2;
    	number3.value= num3;
    	number4.value = num4;
    	number5.value = num5;
    	number6.value = num6;
    
    	first = &number1;
    	number1.nextaddr = &number2;
    	number2.nextaddr = &number3;
    	number3.nextaddr = &number4;
    	number4.nextaddr = &number5;
    	number5.nextaddr = &number6;
    	number6.nextaddr = NULL;
    	
    	printf("\nTHE GRADES ENTERED ARE:\n");
    	
    	display(first);
    	
    	return 0;
    }
    
    void display(struct Grade *contents)
    {
    	while (contents != NULL)
    	{
    		printf("%s\n", contents->value);
    		contents = contents->nextaddr;
    	}
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    struct Grade number1, number2, number3, number4, number5, number6, *first;
    why are you trying to redeclare these variables?

    For the large apart, this code looks fine, except for your repeat of code.. what's that about?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    21
    i don't know i guess i was failing conservative... it just seemed like it needed to be there.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    - remove the 2nd long list of 'struct Grade' declarations
    - include in the 1st long list of 'struct Grade' declarations variable named 'number6'
    - change the printf statement in the while loop to print a decimal number, not a string

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    21
    awesome... it works great! I have sucha hard time finding the little things sometimes, but im getting better. Thanks

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    void display (struct Grade *);
    take this line out of the function. It doesn't go in functions.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    21
    should i put it at the top before int main?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes would be best i think to leave it outside any function, like a global variable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-05-2008, 02:18 AM
  2. C Linked Lists and Structures help!
    By Yorae in forum C Programming
    Replies: 0
    Last Post: 03-07-2006, 09:55 AM
  3. Can i do this: Structures and linked lists help
    By satory in forum C Programming
    Replies: 4
    Last Post: 04-25-2005, 08:49 AM
  4. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  5. C Structures and Linked Lists.
    By bobthebuilder20 in forum C Programming
    Replies: 14
    Last Post: 04-15-2003, 06:00 PM

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