Hello guys am working on a c++ project that search for Student id in the source file,if a match is found
it writes to the output file
after the search those were the found
1 2
John Bill M 1895865 20013647 2001 78 66 80 66 79 75
Frank Libmen M 6546727 20016367 2001 50 78 56 43 33 57
First Name: John
Last Name: Bill
Gender: M
Student ID: 1895865
Course Code: 20013647
Year: 2001
Mathematics 78
English 66
Science 80
Geography 66
Music 79
Art 75
First Name: Frank
Last Name: Libmen
Gender: M
Student ID: 6546727
Course Code:20016367
Year: 2001
Mathematics 50
English 78
Science 56
Geography 43
Music 33
Art 57
1) extend person struct to hold the additional fields, parse them out (line 54ish) and then add something to print them to the file, more or less intact. Its crude, but it should work.
2) a bit of a do-over but here, if you parsed the records into your struct *as you read them* and put them all into a nice vector<person> everyone; it would be a fair amount of work but a lot cleaner than reading the file each time and such. If you have not yet covered vectors/arrays, this would not be helpful advice.
output to text files looks exactly like cout. So you can get it working with cout then replace cout with file and it may be easier to debug etc.
if (!found)
{
notfound << "No NRC match for:" << source_line << endl;
}
else
{
cout << "NRC Match Found for:" << source_line << endl;
output << source_line << endl;
}
In line 3 are you sure that "source_line" is the correct variable? It did not work for me, but I could have the files "input.txt" and "source.txt" wrong. Although based on those names I could be misunderstanding what each file is for.
As mentioned there are several ways that you could do the output in the else part. I tried this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
else
{
cout << "NRC Match Found for: " << source_line << endl;
//output << source_line << endl;
output
<< std::setw(14) << "First Name : " << input_person.firstname << '\n'
<< std::setw(14) << "Last Name : " << input_person.surname << '\n'
<< std::setw(14) << "Gender : " << input_person.gender << '\n'
<< std::setw(14) << "Student ID : " << input_person.id << '\n'
<< std::setw(14) << "Course Code : " << "20013647 This line never read from input" << '\n'
<< std::setw(14) << "Year : " << "2001 This line never read from input" << '\n'
<< "\n The rest of what you need was never read from the file\n\n";
found = false;
}
This produced:
First Name : John
Last Name : Bill
Gender : M
Student ID : 1895865
Course Code : 20013647 This line never read from input
Year : 2001 This line never read from input
The rest of what you need was never read from the file
First Name : Frank
Last Name : Libmen
Gender : M
Student ID : 6546727
Course Code : 20013647 This line never read from input
Year : 2001 This line never read from input
The rest of what you need was never read from the file
If you are not familiar with "std::setw()" you can leave that out and just add spaces to the beginning of each line if you want the (:)s to line up.