Name and File Search and Display trouble

i am having some trouble displaying information from the file in this program when i run the program and select 1 to display all of the students information

it only displays the first instance of all of the information

any help would be appreciated

the database.txt contents contents:

Anthony Rodgers
900235487
Game_Design
Nothing
Online_Class_Python
Hybrid_Advanced_C++
Nothing
Nothing
9_hours
Alex Dunker
900236491
Auto_Enginerring
Nothing
Welding_101
Nothing
Nothing
Mechanics_101
10_hours



Updated Code

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

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

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

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:
    // 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;
}

    // print function
    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;
    }
};

void inputData(vector<Student> &, fstream&);

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()
{
	int choice;

	cout << "\n		     MAIN MENU		" << endl;
	cout << "========================================================" << endl;
	cout << "1. Display the whole student database." << endl;
	cout << "2. Edit Student Information." << endl;
	cout << "3. Search for student by first name." << endl;
	cout << "4. Exit Program." << endl << endl;

	cout << "Enter your choice." << endl;
	cin >> choice;

	if (choice == 1)
	{
		vector<Student> studentInfo;
		fstream dataFile("database.txt");

		while (!dataFile.eof())
		{
			inputData(studentInfo, dataFile);
		}

		// Step through the vector, printing each
		// object's data.
		cout << "\nHere are all of the students information:\n";
		cout << "-----------------------\n";
		for (int i = 0; i < studentInfo.size(); i++)
		{
			studentInfo[i].print();
			cout << "-----------------------\n";
		}

    dataFile.close();
	Menu();
	}
	else
	{
		if (choice == 2)
		{
			cout << "Unable to complete this section.";
			cout << "Redireting back to the main menu.";
			Menu();
		}
		else
		{
			if (choice == 3)
			{
				/*void studentSearch(const vector<Student>& studentInfo, string targetStudent)
					{
						string targetStudentFirstName;
						bool studentFound;

						cout << "Input the first name of the student that you want to find";
						cout << "Student First Name:" << endl;
						cin >> targetStudentFirstName;


					for (int i = 0; i < studentInfo.size(); i++)
					{
					if (studentInfo[i].getFirstName() == targetStudentFirstName){

						studentFound = true;
						studentInfo[i].print();
		}
	}
*/
			}
			else
				if (choice == 4)
				{
					exitProgram();
				}
				else
				{
					cout << "Invalid entry";
					Menu();
				}
		}
	}
}

void inputData(vector<Student> &studentInfo, fstream& afile)
{
    string firstName;	// Holds the students firstName
	string lastName;
	string idNumber;    // Holds the students ID number
	string major;		// Holds their Major
	string M;
	string Tu;
	string W;
	string Tr;
	string F;
	string H;

    getline(afile, firstName);

	getline(afile, lastName);

	getline(afile, idNumber);

	getline(afile, major);

	getline(afile, M);

	getline(afile, Tu);

	getline(afile, W);

	getline(afile, Tr);

	getline(afile, F);

	getline(afile, H);

    // Create a PhoneBookEntry object initialized with
    // this data.
    Student entry(firstName, lastName, idNumber, major, 
		M, Tu, W, Tr, F, H);

    // Store the entry in the vector.
    studentInfo.push_back(entry);
}

void exitProgram()
{
	string responce;

	cout << "Are you sure you want to logout? Y/N" << endl;
	cin >> responce;

	if (responce == "Y" || responce == "y")
    {
        cout << "Logging Out...\n";
        adminLogin();
	}
	else
	{
		if (responce == "N" || responce == "n")
		{
			Menu();
		}
		else
		{
			cout << "Invalid choice.";
			exitProgram();
		}
	}
}
Last edited on
Hi

what are you doing here, studentInfo.pop_back(); ??

You remove this line, 118, from your code :-)

Last edited on
@the rockon7throw

oh i see i took that line of code out of the program and it runs fine thank you

Now i have another question using the same information i now want to search and display the information of a specific student by searching for their first name

iterate through a loop and compare the target name with the data being in the vector, some thing like

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

void somDummyName(const vector<Student> &studentData, const string& targetStudent){
      bool  studentFound = false ;
for (int i = 0; i < studentData.size(); i++)
		{
			// you should implement getName in your Student class
                        if (studentData[i].getName() == targetStudent ){
                          // do some thing here
                           studentFound  = true ;
                          //do some thing other
                          

                         // and return
                           return;
                          }
			
		}

               if(!studentFound ){
                  // some log student not found
               }
}


hope it helps
Last edited on
i have updated the code above display instead of a full name it displays first name and last name separately
It should work in a just simple way like following

1
2
3
4
5
6
7
for (int i = 0; i < studentData.size(); i++){
        if (studentData[i].getFirstName() == targetStudentFirstName ){
                // do some thing here
                 studentFound  = true ;
                  studentData[i].print();
         }
}


you should implement getFirstName in your class

1
2
3
string getFirstName()const{
    return fName;
}


the way you wish to print your data out, should be formated in your print method, centralized there
Last edited on
i am getting an error message i dont understand why its there actually
any help on that

the error is:
main.cpp(131): warning C4018: '<' : signed/unsigned mismatch
main.cpp(220): warning C4018: '<' : signed/unsigned mismatch
main.cpp(225): error C2662: 'Student::print' : cannot convert 'this' pointer from 'const Student' to 'Student &'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void studentSearch(const vector<Student> &studentInfo, string targetStudent)
{
	string targetStudentFirstName;
	bool studentFound;

	cout << "Input the first name of the student that you want to find";
	cout << "Student First Name:" << endl;
	cin >> targetStudentFirstName;


	for (int i = 0; i < studentInfo.size(); i++)
	{
		if (studentInfo[i].getFirstName() == targetStudentFirstName){

			studentFound = true;
			studentInfo[i].print();
		}
	}
}
Last edited on
Hi

change your print method into const , like that way

1
2
3
4
5
6
7
virtual void print() const
 {

// do whatever


}


or implement both versions, and if you implement both versions, it will be disgusted here again :-)
Last edited on
Topic archived. No new replies allowed.