Student Database File Data Troubles

Pages: 12
Sorry for all of the code
I am having alot of trouble with this program. I want to have a student database that doses not use STL. I am mainly trying to just display the file contents first and then I would like to search the file for a specific students information via first name. I also want to use class files to help with the structure. Any help will be appreciated
database.txt:
Anthony
Rodriguez
900235487
Game_Design
Nothing
Introduction_Python
Advanced_C++
Nothing
Nothing
9
Alex
Dunker
900236491
Auto_Enginerring
Nothing
Welding_101
Nothing
Nothing
Mechanics_101
10

Main.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <cstring>
#include "student.h"
#include "graduate.h"
#include "undergrad.h"
#include <fstream>
#include <iomanip>

using namespace std;

const string user = "grader";
const string password = "12345";

void adminLogin();
void Menu();
void exitProgram();

int main()
{

	adminLogin();

	return 0;
}

void adminLogin()
{
    string name, pass;

	cout << "			   CSU			" << endl;
	cout << "		Adminsitrator Login		" << endl;
	cout << "-------------------------------------------" << endl;
    cout << "Username: ";
    cin >> name;
    cout << "Password: "; 
    cin >> pass;

    if (name == user & pass == password)
    {
        cout << "\nYou are logged in!";
        Menu();
	}
	else
	{
        cout << "\nInvalid Username and/or Password..." << endl;
		adminLogin();
	}

}

void Menu()
{

	//string sFName, sLName, sID, sM, scrM, scrTu, scrW, scrTh, scrF, cH, sY, sD, sTh;
	string menuChoices[] = {"Display Student Records", "Edit Student Records", 
		"Search Student(first name)", "Exit Program"};
	for(int z=0; z<4; z++)
	{
		cout << z+1 << "\t\t" << menuChoices[z] << endl;
	}

	int userChoice;
	cout << "(Please enter a number) :";
	cin >> userChoice;

	switch (userChoice)
	{

	case 1:
		fstream database("database.txt");
		string textLine;
			while (!database.eof())
			{
				getline (database, textLine);
				cout << textLine << endl;
			}
			database.close();
		break;
	}

}


Student.h:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <iostream>

using namespace std;

class Student
{
private:
    string fName;         // Student Name
	string lName;
    string idNumber;      // Student ID Number
	string major;		  // Student Major
	string schM;		  // Monday Course
	string schTu;		  // Tuesday Course
	string schW;		  // Wednesday Course
	string schTh;		  // Thursday Course
	string schF;		  // Friday Course
	string crH;			  // Course Hours

public:

	Student()
	{
		fName = "";
		lName = "";
		idNumber = "";
		major = "";
		schM = "";
		schTu = "";
		schW = "";
		schTh = "";
		schF = "";
		crH = "";
	}
    // Constructor
    Student(string fn, string ln, string idn, string m, 
		string M, string Tu, string W, string Tr, string F, string H)
    {
        fName = fn;
		lName = ln;
        idNumber = idn;
		major = m;
		schM = M;
		schTu = Tu;
		schW = W;
		schTh = Tr;
		schF = F;
		crH = H;	
    }

	string getFirstName()const
	{ return fName; }

	string getLastName()const
	{ return lName; }

	string getID()const
	{ return idNumber; }

	string getMajor()const
	{ return major; }

	string getMonday()const
	{ return schM; }

	string getTuesday()const
	{ return schTu; }

	string getWednesday()const
	{ return schW; }

	string getThursday()const
	{ return schTh; }

	string getFriday()const
	{ return schF; }

	string getCourseHrs()const
	{ return crH; }

	virtual void print()
    {
        cout << "Student First Name: " << fName << endl
			 << "Student Last Name: " << lName << endl
             << "Student ID Number: " << idNumber << endl
			 << "Student Major: " << major << endl
			 << "Monday Class: " << schM << endl
			 << "Tuesday Class: " << schTu << endl
			 << "Wednesday Class: " << schW << endl
			 << "Thursday Class: " << schTh << endl
			 << "Friday Class: " << schF << endl
			 << "Course Hours: " << crH << endl;
    }
};
#endif 


Undergrad.h:
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
#ifndef UNDERGRAD_H
#define UNDERGRAD_H
#include "student.h"
#include <string>
#include <iostream>

using namespace std;

class Undergrad : public Student
{
private:
	string year;

public:

	Undergrad() : Student()
	{
		year = "";
	}

	Undergrad(string y) : Student()
	{
		year = y;
	}

	string getYear() const
	{ return year; }

	virtual void print()
	{
		cout << "Current year in school" << year << endl;
	}
};
#endif 


Graduate.h:
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
#ifndef GRADUATE_H
#define GRADUATE_H
#include "student.h"
#include <string>
#include <iostream>

using namespace std;

class Graduate : public Student
{
private:

	string degree;
	string thesis;

public:

	Graduate() : Student()
	{
		degree = "";
		thesis = "";
	}

	Graduate(string d, string t) : Student()
	{
		degree = d;
		thesis = t;
	}

	string getDegree()const
	{ return degree; }

	string getThesis()const
	{ return thesis; }

	virtual void print()
	{
		cout << "Aspiring Degree :" << degree << endl;
		cout << "Thesis Topic :" << thesis << endl;
	}
};
#endif 
Last edited on
You have posted 255 lines of code. What isn't working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
fstream database("database.txt");
fstream database;
database.open("database.txt");
		
string textLine;
			
while (!database.eof())
{
   getline (database, textLine);
   cout << textLine << endl;
}
 
database.close();
break;
Last edited on
Sorry My main issue is that I do not know how to take information from a file (line by line) and have a class take that file input and at least display it to the user and then continue to use that same information to search for a specific student. Sorry again for not goin ginto detail
For displaying the database, you can have a member function do the same thing you have here:

1
2
3
4
5
virtual void print()
{
   cout << "Aspiring Degree :" << degree << endl;
   cout << "Thesis Topic :" << thesis << endl;
}


with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void printDatabase()
{
   fstream database;
   database.open("database.txt");
		
   string textLine;
			
   while (!database.eof())
   {
      getline (database, textLine);
      cout << textLine << endl;
   }
 
   database.close();
}


and if you want to search by name, you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
do
{
   getline (database, textLine);

   if (textLine == "John Smith")
     break;

} while (!database.eof());

if (textLine == "John Smith")
  cout << textLine;

else
  cout << "There is no such student!";
Last edited on
This is what I understand where you are getting at:

First: I would need to make a function in either the Student class or in the main
that is pretty much a copy of the printDatabase function you have there

Second: I need to assign each string like Anthony in the database.txt file to the Student string variable fName and so on for each separate student
I would need to make a function in either the Student class or in the main that is pretty much a copy of the printDatabase function you have there
If you want one of your classes to have this function, then it would have to be a class member function. Otherwise it could just be in main.

I need to assign each string like Anthony in the database.txt file to the Student string variable fName and so on for each separate student
You could make a function called findStudent() which returns true if a student is in the database and false if the student isn't in it. Kind of like my do while loop I have above.
I would like to have the print functions be in the class files.
Would this be what they would look like for the Student class ?
1
2
3
4
5
void Student::setfName(string fName)
{
    getline(line,firstname);
    cout<< "First Name :" << firstname << endl;
}


I would want this to be used to display the first name of each student
You will need to search the file for the name, or else you will only capture the first line of the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Student::setfName(string fName)
{
   string textLine;

   do
   {
      getline (database, textLine);

      if (textLine == fName)
        break;

   } while (!database.eof());

   if (textLine == fName)
     cout<< "First Name :" << fName << endl;

   else
     cout << "There is no such student!";
}


Except take into consideration what happens if there are more than one string on a line in the file, such as

John Smith Jacob Nelson


Then your getline would set firstname to "John Smith Jacob Nelson". Think about that as well.
Last edited on
I do not intend on anything sharing a line except if it is part of a whole output

Correct me if i am wrong so this code will look something like this:

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
class Student{

//Constructors
//Setters and Geters

void Student::setfName(string fName)
{
   do
   {
      getline (database, textLine);

      if (textLine == fName)
        break;

   } while (!database.eof());

   if (textLine == fName)
     cout<< "First Name :" << fName << endl;

   else
     cout << "There is no such student!";

string Student::getfName()
{ return fn; }
}
Last edited on
I just want to get the database to display right now that is mainly where my problem is right now.
Last edited on
Yes this will work, if you have textLine as a data member of your class. Or you can just declare it as a local variable in this function as I have.

And if this implementation is inside the class, : class Student{ you will not need void Student::.

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
class Student
{

   //Constructors
   //Setters and Geters

   void Student::setfName(string fName)
   void setfName(string fName)
   {
      string textLine;

      do
      {
         getline (database, textLine);

         if (textLine == fName)
           break;

      } while (!database.eof());

      if (textLine == fName)
        cout << "First Name :" << fName << endl;

      else
        cout << "There is no such student!";

   string Student::getfName()
   string getfName()
   { return fn; }
}
Last edited on
Here is the updated Student class file but I am getting a small error:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Student
{
private:
    string fName;         // Student Name
	string lName;
    string idNumber;      // Student ID Number
	string major;		  // Student Major
	string schM;		  // Monday Course
	string schTu;		  // Tuesday Course
	string schW;		  // Wednesday Course
	string schTh;		  // Thursday Course
	string schF;		  // Friday Course
	string crH;			  // Course Hours

public:

	Student()
	{
		fName = "";
		lName = "";
		idNumber = "";
		major = "";
		schM = "";
		schTu = "";
		schW = "";
		schTh = "";
		schF = "";
		crH = "";
	}
    // Constructor
    Student(string fn, string ln, string idn, string m, 
		string M, string Tu, string W, string Tr, string F, string H)
    {
        fName = fn;
		lName = ln;
        idNumber = idn;
		major = m;
		schM = M;
		schTu = Tu;
		schW = W;
		schTh = Tr;
		schF = F;
		crH = H;	
    }

	string getFirstName()const
	{ return fName; }

	string getLastName()const
	{ return lName; }

	string getID()const
	{ return idNumber; }

	string getMajor()const
	{ return major; }

	string getMonday()const
	{ return schM; }

	string getTuesday()const
	{ return schTu; }

	string getWednesday()const
	{ return schW; }

	string getThursday()const
	{ return schTh; }

	string getFriday()const
	{ return schF; }

	string getCourseHrs()const
	{ return crH; }

	void setfName(string fName)
{
	fstream database;
    database.open("database.txt");
    string textLine;

   do
   {
      getline (database, textLine);

      if (textLine == fName)
        break;

   } while (!database.eof());

   if (textLine == fName)
     cout<< "First Name :" << fName << endl;

   else
     cout << "There is no such student!";

   string getfName()
   { return fn; }// Getting an error here that says Expected a ";"
}

	virtual void print()
    {
        cout << "Student First Name: " << fName << endl
			 << "Student Last Name: " << lName << endl
             << "Student ID Number: " << idNumber << endl
			 << "Student Major: " << major << endl
			 << "Monday Class: " << schM << endl
			 << "Tuesday Class: " << schTu << endl
			 << "Wednesday Class: " << schW << endl
			 << "Thursday Class: " << schTh << endl
			 << "Friday Class: " << schF << endl
			 << "Course Hours: " << crH << endl;
    }
};
#endif 
what is the error?
here they are:

student.h(108): error C2601: 'getfName' : local function definitions are illegal

student.h(108): error C2065: 'fn' : undeclared identifier
getfName() is its own function.

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
void setfName(string fName)
{
	fstream database;
    database.open("database.txt");
    string textLine;

   do
   {
      getline (database, textLine);

      if (textLine == fName)
        break;

   } while (!database.eof());

   if (textLine == fName)
   {
     cout<< "First Name :" << fName << endl;
     fn = fName; // set your data member;
   }

   else
     cout << "There is no such student!";

}  // getfName is its own function.

string getfName()
   { return fn; }
} // remove 


student.h(108): error C2065: 'fn' : undeclared identifier
You need to have fn be a data member of the class.
Last edited on
would it be okay to use the getter on line 46:

1
2
string getFirstName()const
	{ return fName; }
Try it.
the code works fine now after an adjustment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void setFirstName(string fName)
{
	fstream database;
    database.open("database.txt");
    string textLine;

   do
   {
      getline (database, textLine);

      if (textLine == fName)
        break;

   } while (!database.eof());

   if (textLine == fName)
     cout<< "First Name :" << fName << endl;

   else
     cout << "There is no such student!";
	}

   string getFirstName() const
   { return fName; }



can this same stream of code be used to display the other parts of the text file like the last name and such

an example of that type of code i hope:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void setLastName(string lName)
{
	fstream database;
    database.open("database.txt");
    string textLine;

   do
   {
      getline (database, textLine);

      if (textLine == lName)
        break;

   } while (!database.eof());

   if (textLine == lName)
     cout<< "Last Name :" << lName << endl;

   else
     cout << "There is no such last name!";
	}

   string getLastName() const
   { return lName; }
Last edited on
Yes that will work.
ok so now I have a few questions of you dont mind

first how would the full display look with these functions

second how would i reference these functions in the main during my menu function

third how would i add new student data via inputs to the file and be able to be saved and still displayed when i wish it
Pages: 12