passing struct pointers to make tables/files

This is a discussion on passing struct pointers to make tables/files within the C++ Programming forums, part of the General Programming Boards category; ok im banging my head against the wall on this one.... here's the problem: say I'm a professor and i ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    passing struct pointers to make tables/files

    ok im banging my head against the wall on this one....

    here's the problem:

    say I'm a professor and i keep a record of all my students, here is the info i keep:

    first name
    last name
    ssn
    phone numb
    grades for 5 quizzes
    grade for final exam
    final grade average

    1st thing I need to do is to make a structure with this info and here it is:

    Code:
     struct STUDENT_STRUCT
    	{
    		char last_name [21];
    		char first_name [21];
    		char social [12];
    		char phone [9];
    		int quiz [5];
    		int final;
    		int final_grade;
    	};
    Next, I need to write a function that prompts the user for all info for one student except the final grade. The problem is, that the function's only argument has to be a pointer to the structure of the type above.

    I know this is probably wrong but here is the prototype, the call to the function from main, and the actual function...


    Code:
     STUDENT_STRUCT  user_prompt(STUDENT_STRUCT*);
    
    ---------------------------------------------------------------------------------
    
    	cout << "\nWould you like to enter your first student's "
    		<< "information now?  Enter Y or N\n";
    	cin.getline (response, 11);
    	cin.ignore(80, '\n');
    
    	while (toupper(*response) == 'Y')
    	{
    		while (row < TABLE_SIZE)
    		{
    			user_prompt(&structure);
    			++row;
    			++count;
    			cout << "Enter another student? Enter Y or N";
    			cin.getline (response, 11);
    			cin.ignore(80, '\n');
    		}
    		else
    			break;
    	}
    
    ------------------------------------------------------------------------------------
    
    void user_prompt (STUDENT_STRUCT* stud_ptr)
    {
    	int x;
    	
    
    	
    	cout << "\nEnter student's last name: ";
    	cin.getline (stud_info.last_name, 21);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's first name: ";
    	cin.getline (stud_info.first_name, 21);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's social security number: ";
    	cin.getline (stud_info.social, 12);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's phone number: ";
    	cin.getline (stud_info.phone, 9);
    	cin.ignore (80, '\n');
    	for (x=0;x<5;++x)
    	{
    		cout << "\nEnter student's grade for quiz " << x+1 << ": ";
    		cin >> stud_info.quiz[x];
    	}
    	cout << "\nEnter the student's final exam grade: ";
    	cin >> stud_info.final;
    
    }
    as you can see I'm already hitting brick walls thus far....

    next, i ned to write a function whose only argument, once again, is a pointer to the same arguement type above(STUDENT_STRUCT), and computes the final grade of the student as follows: the final grade is 75 percent of the quiz average plus 25 percent of the final exam

    finally, the program i write prompts the user to input data on as many as 25 students, which should be stored in a table. the program uses the 2 functions to load and manipulate the grades and finally spit out a report that lists the students in alphabetical order and display the quiz average, the final exam grade, and the final quiz average. Also, the program must include the option to read/write to a text file.

    The problem I am having is that I don't know how to go back and forth between the functions. Since I am regulated to pointers, Im not sure i can figure out how to pass tables or parts of tables back and forth.

    Anyone who can help me would be my own personal saviour. I included what i have so far in its entirety just in case you wondered how i put it together so far. Yes, this program is far from finished and it's very f*^%$ed up .

    Code:
    /*this program computes a final average for marketing students
    using structures, pointers, and functions.*/
    
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define TABLE_SIZE 25
    
    struct STUDENT_STRUCT
    	{
    		char last_name [21];
    		char first_name [21];
    		char social [12];
    		char phone [9];
    		int quiz [5];
    		int final;
    		int final_grade;
    	};
    
    STUDENT_STRUCT  user_prompt		(STUDENT_STRUCT*);
    int				grade_computer	(STUDENT_STRUCT*);
    int				quiz_computer	(STUDENT_STRUCT*);
    
    int main ()
    {
    	
    	
    	STUDENT_STRUCT	structure;
    	int row = 0;
    	int count = 1;
    	char response[8];
    	
    
    
    	cout << "Welcome, Dr. Jacobs, to the marketing 101 grade "
    		<< "computation program.  (hit any key when ready)\n";
    	cin.get();
    	cin.ignore(80, '\n');
    	cout << "\nWould you like to enter your first student's "
    		<< "information now?  Enter Y or N\n";
    	cin.getline (response, 11);
    	cin.ignore(80, '\n');
    
    	while (toupper(*response) == 'Y')
    	{
    		while (row < TABLE_SIZE)
    		{
    			user_prompt(&structure);
    			++row;
    			++count;
    			cout << "Enter another student? Enter Y or N";
    			cin.getline (response, 11);
    			cin.ignore(80, '\n');
    		}
    		else
    			break;
    	}
    
    	cout << "Final student class averages are as follows: \n\n";
    	row = 0;
    	
    	for (row=0;row<count;++row)
    	{
    		stud_table[row].final_grade = grade_computer(stud_table[row]);
    		stud_table[row].quiz = quiz_computer(stud_table[row]);
    	}
    
    	return 0;
    }
    
    void user_prompt (STUDENT_STRUCT* stud_ptr)
    {
    	int x;
    	
    
    	
    	cout << "\nEnter student's last name: ";
    	cin.getline (stud_info.last_name, 21);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's first name: ";
    	cin.getline (stud_info.first_name, 21);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's social security number: ";
    	cin.getline (stud_info.social, 12);
    	cin.ignore (80, '\n');
    	cout << "\nEnter student's phone number: ";
    	cin.getline (stud_info.phone, 9);
    	cin.ignore (80, '\n');
    	for (x=0;x<5;++x)
    	{
    		cout << "\nEnter student's grade for quiz " << x+1 << ": ";
    		cin >> stud_info.quiz[x];
    	}
    	cout << "\nEnter the student's final exam grade: ";
    	cin >> stud_info.final;
    
    }
    
    int grade_computer (STUDENT_STRUCT* stud_ptr)
    {
    	
    
    	int quiz_ave,
    		quiz_total,
    		q,
    		final_ave;
    
    	for (q=0,q<5,++q)
    	{
    		quiz_total += stud_table.quiz[q];
    	}
    		quiz_ave = quiz_total/(q+1);
    		final_ave = ((.75)*(quiz_ave)) + ((.25)*(stud_table.final));
    		return final_ave;
    }
    
    int quiz_computer (STUDENT_STRUCT* stud_ptr)
    {
    	
    
    	int qquiz_ave,
    		qquiz_total,
    		row,
    		qq;
    	for (row=0,row<25,row
    
    
    		for (qq=0,q<5,++qq)
    		{
    			qquiz_total += stud_table[row].quiz[qq];
    		}
    	
    	quiz_ave = qquiz_total/(q+1);
    	return quiz_ave;
    }
    cout << "\aBIOTCH";

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Do you know what constructors are?

    Do you know what arrays are?

    In a constructor, you could get the data and assign it to the data members, and also make the calculation for the final grade and assign it to the appropriate data member.

    You can create an array of 25 student structures.

    You don't seem to know how to use or define functions. Here's your declaration:

    STUDENT_STRUCT user_prompt (STUDENT_STRUCT*);

    Here's the function header for your definition:

    void user_prompt (STUDENT_STRUCT* stud_ptr)

    and they don't have the same return type. Also, stud_ptr is the function parameter, yet you don't use the parameter in your definition, and if you don't use it, you shouldn't have it as a parameter.

    The harsh reality is, you don't know the basics about functions, and so this program is beyond your understanding at this time. I suggest you practice using a function that is very simple, takes no parameters, returns void, and displays a simple message like "Hello world". Practice calling it from main(). Then create a function that takes a single int parameter, returns void, and displays the argument to the screen. Practice calling that function from main(). When you can do that, try creating a function that takes an int argument, multiplies it by 2, and returns the result. Call the function from main(), and in main() display the return value.

    Then move onto pointers. Try passing a pointer to a simple two member struct to a function that returns void, and in the function change both members of the struct using the pointer, and in main() display the data members of the struct to verify they were changed. After you can do those simple things, then go back to trying your current program.
    Last edited by 7stud; 04-26-2003 at 06:24 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    the reason that the function are screwed up is because i started out trying to attack this from a different angle. I was just changing things here and there and I haven't gone back through the program to change the functions yet.

    I know the basic ideas and uses of functions

    return values FUNCTION_NAME (arguement, arguement)

    I know that void return means nothing gets passed back to main
    I know that the arguements come from the calling function
    cout << "\aBIOTCH";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. array of pointers of a struct
    By paulur in forum C Programming
    Replies: 18
    Last Post: 04-14-2006, 07:17 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. Passing a struct
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 03-14-2005, 10:02 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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