0

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

The text file

Output aftersearching

Dante
  • 87
  • 10

1 Answers1

1

in

if ( search[i] == line[i]){

you use i (int var) but you never define i = 0 and use i++. Var i contain "random" number and program fail at compering search[i] == line[i], becouse i is bigger than 20.

also, at the end of the line, there is no ',' but '\n'.

try this:

#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, ','); // read first line to first ','
            for (i = 0;line[i] == search[i];i++)
            {
                if (search[i] == '\0') // if true search and line is same
                {
                    // print all info
                    cout << "Match found!" << endl;
                    cout << line << endl;
                    infile.getline(line, 500, ',');
                    cout << line << endl;
                    infile.getline(line, 500, ',');
                    cout << line << endl;
                    infile.getline(line, 500, ',');
                    cout << line << endl;
                    infile.getline(line, 500, '\n'); // end of line
                    cout << line << endl;
                    return 1;
                }
            }
            // no match
            for (int j = 0;j < 3;j++) infile.getline(line, 500, ','); // skip the line
            infile.getline(line, 500, '\n'); // we reach end of line
        }
        cout << "Match not found!" << endl;
    }
    else
    {
        cout << "Unable to open: students.txt" << endl;
    }
    infile.close();
    return 0;
}

if you have question about code ask in comment.

Atom
  • 34
  • 4
  • Thank you, this helped. But I don't understand these lines: for (int j = 0;j < 3;j++) infile.getline(line, 500, ','); // skip the line infile.getline(line, 500, '\n'); // we reach end of line – Dante Jun 30 '18 at 12:39
  • Would the j<3 change if I had more entries in the text file? I'm guessing that's what it's for. – Dante Jun 30 '18 at 12:44
  • for (int j = 0;j < 3;j++) infile.getline(line, 500, ','); // skip the line infile.getline(line, 500, '\n'); // we reach end of line --- in this two lines you know that search and line is diferent, so you skip all data on that line and go to next line. (now, when I thinking about it, you can remove the: "for (int j = 0;j < 3;j++) infile.getline(line, 500, ','); // skip the line," becouse, the second line is searching for the end of line.) Hope that help. – Atom Jul 01 '18 at 09:29
  • Okay, I understand now: Thank you again – Dante Jul 02 '18 at 15:40