Student Database: help with ifstream and ofstream operators

Hello, i've been working on a student data base that reads in the students name, birth date, social security, and department name (or major). I have all these items in the header files respectively; nameType, dateType, personType, and studentType.

I am now to create another header file called HWONEHEADER that contains the functions showMenu, loadStudent, insertStudent, searchByName, and SaveStudents. This is how far ive gotten

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef HW
#define HW

#include <iostream>
#include <fstream>
#include <iomanip>
#include "studentType.h"


using namespace std;

ifstream inFile;
ofstream outFile;

void showMenu()
{
	cout<<"\nWelcome to the student database\n";
	cout<<"1. Add a student into the list\n";
	cout<<"2. List all students\n";
	cout<<"3. Search for a student by name\n";
	cout<<"4. Save the student list\n";
	cout<<"5. Exit\n";
}

void loadStudents()
{
	studentType Student;
	int total = 0;
	int id = 0;

	
	inFile.open("students.dat");
	
	while(!inFile.eof())
	{
		inFile>>Student;
		
		if (id != Student.getSSN())
		{
			
			total++;
			id = Student.getSSN();
		}
	}
	inFile.open("students.dat");
	
	cout<<endl<<total<< " students have been read into the list\n\n";
		inFile.close();
} 

Here is the cpp file i have.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int main()
{
	
	studentType department;
	cin>>department;
	cout<<department;
	
	
	
	/*
	
	
	bool changed = false;
	studentType loadStudents(50);


	char choose; //declare option choices for showMenu
//loop
	do
	{
		showMenu(); //show the menu
		cout<<"Please choose an option from the menu: "; 
		cin>>choose; 

		switch (choose) //case for handleing showMenu
		{
		case '1': //input student
		case 'a':
		case '2': //print the list of students
		case 'l':
		case 'L': studentType.printStudents(); //fuction to print all the students
		case '3': // search student by name
		case 'h':
		case 'H': searchByName(); //brings up the function searchByName

			break;
		case '4': //changed list
		case 'v': 
		case 'V': if (changed) //check if the list has been changed
					  saveStudents();
				  else
					  cout<<"\nNo changes need to be saved\n\n";
			break;
		case '5': //exit the system
		case 'e':
		case 'E':
			
			break;

		default:
			cout<<"Invalid option, please enter another option.\n\n";
		}
	}
	
	while (choose != '5' && choose != 'e' && choose != 'E');
	
	if (changed) //if the list has been changed then save it
		saveStudents();
	cout<<"Thank you for using the student database!\n\n";
	
	*/
	return 0;

	
}

I commented out the switch statement that intiates the menu.
Here is the studentType header file which works perfectly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef studenttype
#define studenttype

#include <iostream>
#include "personType.h"


using namespace std;

class studentType : public personType
{
	friend ostream& operator<<(ostream&, const studentType&);
	friend istream& operator>>(istream&, studentType&);

public:
	void setDept(string); //set the department
	string getDept() { return dpmt; }; //get the department 
	
//constructors
	studentType();
	studentType(int, string, string);
private:
	string dpmt;
};
/**************************************
Overload iostream operator <<
**************************************/
ostream& operator<<(ostream& os, const studentType& aStudent)
{
	
	os<<(personType&)(aStudent)<<" "<<aStudent.dpmt;
	
	return os;
}
/**********************************************
Overload iostream operator >>
**********************************************/
istream& operator>>(istream& is, studentType& aStudent)
{
	personType& aPerson = (personType&)aStudent;
	is>>aPerson;
	cout<<" Please enter the department. ";
	is>>aStudent.dpmt;

	return is;
}
//set the students department
void studentType::setDept(string dept)
{
	setDept(dpmt);
}
//default constructor
studentType::studentType()
{
	dpmt = "jack"; //sets constructor to NULL
}
//constructor with parameters
studentType::studentType(int id, string first, string last)
	
{
	dpmt = "hello"; //set constructor to NULL
}

#endif 

I need help with the HWONEHEADER using the ifstream and ofstream operators to load all the students information into a file called student.dat I'am extremely confused because i can't find anything in my textbook about using ifstream and ofstream operators. Any input would be helpful. Thank you.
Last edited on
Can someone help me create the loadStudents and insertStudents in my HWONEHEADER file is basically what im asking. And making a menu using that switch statement in the cpp file that i have commented out.
Right now i know i need to make and array, but i need help with creating my loadStudents. Theres absolutely nothing in my book about ifstream and ofstream operators and creating dat files. Can someone please give some input? I didnt work hours on this stupid thing just to get stuck.
Last edited on
this is how far ive gotten.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef HW
#define HW

#include <iostream>
#include <fstream>
#include <iomanip>
#include "studentType.h"


using namespace std;



void showMenu()
{
	cout<<"\nWelcome to the student database\n";
	cout<<"1. Add a student into the list\n";
	cout<<"2. List all students\n";
	cout<<"3. Search for a student by name\n";
	cout<<"4. Save the student list\n";
	cout<<"5. Exit\n";
}

void loadStudents(ifstream& inFile, studentType list[], int listSize)
{
	int index;
    int quater;
	ofstream outFile;
	 //total = 0;
	//int id = 0;

	
	inFile.open("students.dat");
	
	for(index = 0; index < listSize; index++)
		inFile >> list[index].setDept[index];
	for(quater = 0; quater < 4; quater++)
		list[index].setDept[quater] = NULL;


}

The showMenu function works along with all my other header files. I need help with creating an array in my function loadStudents. I havent learned arrays very well and my book has nothing on how to work the ifstream ofstream operators.
Topic archived. No new replies allowed.