structures and functions

This is a discussion on structures and functions within the C Programming forums, part of the General Programming Boards category; I am trying to create a program with a structure that stores the information of a student. It must also ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Question structures and functions

    I am trying to create a program with a structure that stores the information of a student. It must also include a function that prompts the user for the information of the student. The functions only argument is to be pointer to the structure. I am completly lost, (as I think my code shows). Can someone please tell where I've screwed up at? My compiler won't even compile this( I'm using Borland 5). I've never had that happen before.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct
       {
    	char firstname[25];
    	char lastname[25];
       }name_struct;
    
    typedef struct
       {
    	name_struct	name;
    	char		ssn[10];
    	char		phone[10];
    	int		quiz[5];
       }student;
    
    void get_student_details(student*);
    
    main()
    {
         student student;
         get_student_details(&student);
        printf("\n\nThe student's name is: %s %s", student.firstname, student.lastname);
    }
    	void get_student_details(student* student_details)
    	{
    	      printf("\nEnter the first name of the student:");
    		scanf("%s", student_details->firstname);
    	      printf("\nEnter the last name of the student:");
    		scanf("%s", student_details->lastname);
    	      printf("\nEnter the social security number:");
    		scanf("%s", student_details->ssn);
    	      printf("\nEnter the student's phone number:");
    		scanf("%s", student_details->phone);
    	      printf("\nEnter the student's 5 quiz grades:");
    		for(number=0; number<5; ++number)
    		  {
    		    printf("\nEnter the grade for %d:", quiz+1);
    		     scanf("%lf", student_details->quiz);
    		  }
    	}

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your problem is that you've forgotten you have nested structures. You're missing a level of indirection:

    scanf("%s", student_details->name.firstname );

    See? That should do the trick.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Unhappy still won't compile

    Quzah,
    I made the change, of course I feel stupid, I always seem to miss the obvious. But it still won't compile. Is it my program or has my compiler finally had enough of C.

    Thanks for your help, I would be lost without ya'll.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    edited code

    Sorry, here's the latest.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct
    {
    	char firstname[25];
    	char lastname[25];
    }name_struct;
    
    typedef struct
    {
    	name_struct	name;
    	char		ssn[10];
    	char		phone[10];
    	int		quiz[5];
    }student;
    
    void get_student_details(student*);
    
    main()
    {
    	student student;
    	get_student_details(&student);
    	printf("\n\nThe student's name is: %s %10s", student.firstname, student.lastname);
    }
    	void get_student_details(student* student_details);
    	{
    		printf("\nEnter the first name of the student:");
    		scanf("%s", student_details->name.firstname);
    		printf("\nEnter the last name of the student:");
    		scanf("%s", student_details->name.lastname);
    		printf("\nEnter the social security number:");
    		scanf("%s", student_details->student.ssn);
    		printf("\nEnter the student's phone number:");
    		scanf("%s", student_details->student.phone);
    		printf("\nEnter the student's 5 quiz grades:");
    		for(number=0; number<5; ++number)
    		  {
    		    printf("\nEnter the grade for %d:", quiz+1);
    		     scanf("%lf", student_details->student.quiz);
    		  }
    	}

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    ???

    Here's where I'm at now. I realized I had not declared 'number in my function, so I made that change also. But my compiler still starts but will not finishing compiling. I'm about ready to throw in the towel on this one. Structures have completly done me in.

    Code:
    #include<stdio.h>
    
    typedef struct
    {
    	char firstname[25];
    	char lastname[25];
    }name_struct;
    
    typedef struct
    {
    	name_struct	name;
    	char		ssn[10];
    	char		phone[10];
    	int		quiz[5];
    }student;
    
    void get_student_details(student*);
    
    main()
    {
    	student student;
    	get_student_details(&student);
    	printf("\n\nThe student's name is: %s %10s", student.firstname, student.lastname);
    }
    	void get_student_details(student* student_details);
    	{
    		int number;
    		printf("\nEnter the first name of the student:");
    		scanf("%s", student_details->name.firstname);
    		printf("\nEnter the last name of the student:");
    		scanf("%s", student_details->name.lastname);
    		printf("\nEnter the social security number:");
    		scanf("%s", student_details->student.ssn);
    		printf("\nEnter the student's phone number:");
    		scanf("%s", student_details->student.phone);
    		printf("\nEnter the student's 5 quiz grades:");
    		for(number=0; number<=5; ++number)
    		  {
    		    printf("\nEnter the grade for %d:", number+1);
    		     scanf("%lf", &student_details->student.quiz[number]);
    		  }
    		  return 0;
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. COntrol structures and functions questions
    By angelicscars in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 11:50 AM
  2. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 12:22 PM
  3. Array of Structures and Functions
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 08:06 AM
  4. passing array structures to functions
    By lukejack in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 03:17 PM
  5. data structures / hash functions
    By rickc77 in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 01:54 PM

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