I have made a text file and added some data to it. I'm trying to search the text file for, in this case Student ID, and output the line that matches that Student ID. Else output "Student not found"
I have managed to search and output, but I cant output the specific line with the searched id that matches.
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char line[500];
char search[20];
int i;
cout<<endl<<"Student Details"<<endl<<endl;
ifstream infile;
infile.open("students.txt");
cout<<"Search: ";
cin>>search;
if (infile.is_open() ){
while ( !infile.eof() ){
infile.getline(line, 500, ',');
if ( search[i] == line[i]){
while ( !infile.eof() ){
infile.getline(line, 500, ',');
cout<<line<<endl;
}
}
}
}
infile.close();
}
This is the type of output I'm trying to get after searching
ID: H173770
Name: Dante Mishima
Age: 20
Course: Web Design
Address: 13 Grimmauld Place