I am working on a program that will simulate a simple student information system. The program will need to behave as follows:
a - add a student
b - display all student information
c - display a single students information
d - generate a grade report
q - quit
Currently I am working on the "add a student" function. The process I must follow is as follows:
1. Prompt the user to add the name of a student (Last, First).
2. The addUser() function will call the checkUser() function to open a text file and search to see if that student exists.
3. If the user does not exist, a generateID() function is called that will generate a random, five digit ID.
4. Finally, the addUser() function will prompt the user to enter a series of test scores and homework scores. All information (name, ID, scores) is then written to the student.txt file.
Simple enough. However, I am trying to test out a few lines of code that will actually search the text file for me.
I need it to be in two separate functions (hence why I have divided it up), so here is my code as of now:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int search(void);
int main (void)
{
cout << search() << endl;
return 0;
}
int search(void)
{
fstream checkStream;
string searchString;
string lineOfText;
cout << "Please enter a valid name to search for: ";
getline(cin, searchString);
checkStream.open("student.txt", ios::in);
for(;;)
{
getline(checkStream, lineOfText);
if (checkStream.eof()) break;
if (lineOfText.find("searchString", 0) != string::npos)
{
return 1;
break;
}
}
cout << "Done searching..." << endl;
checkStream.close();
return 0;
}
and my student.txt file is as follows:
Code:
Sparrow, Jack
Turner, Will
Snyder, Quinn
Snyder, Melody
Basset, Theodore
However, my code will not output a found message when the string is found. Something is not being passed correctly. Can anyone give me some insight on this problem?
q.