Search and read columns in text file

Hi all, I am new to C++ and I have to create a program for my school project. The program should be able to search an ID from a text file and then display the name of the person associated with the ID. The problem is, I didnt come for the lesson on text file! Hence, I need some help!! Below here is my text file. (The names and ID are random so pls dont mind them)


NAME ID D.O.B
Gabriel S1234562I 21 Dec 1995
Nicholas Yang S9123212U 12 Dec 1998
Joel Lok S8025787I 5 Apr 1991
Cynthia Saram S4352145N 19 Jun 1995
Rebecca Kwok S7614346N 16 Nov 1976
Nicole Chee S7312948D 23 Oct 1992
Henry G9597983R 15 Sept 1990
Justen Aw G9592321T 12 Mar 2000
Hanna Choo S8275095J 5 Apr 1970
Thaqif Othman S7712840T 1 Dec 1989
Imran Hazirah S3306682W 2 May 1983
Spence S3334682W 3 Jan 1985
Amira S8732649Y 24 Feb 1996
Ahmed Best T9823798U 27 Jul 1999
Lauren Yoo S8963447O 14 Jul 1976
Chewbacca G0837321P 11 Jan 1991
Shaziri S9312331H 10 Dec 1983
Qasimy S3894324P 31 Jul 1983
Last edited on
http://bfy.tw/3gnD

https://goo.gl/ffB1lh

It's 2016. There are enough resources online.
Last edited on
Yeah thanks for making me realize how un-resourceful I am
It looks as though the file uses the tab character '\t' as a separator between fields. That's useful since the name and date fields contain spaces.

I suggest you look at the second example under the heading "text files" in the tutorial, for a way to read the file line by line.
http://www.cplusplus.com/doc/tutorial/files/

Within each line you might parse it into fields by using a std::istringstream, then you can use getline() with the tab character as a delimiter to get individual fields.

http://www.cplusplus.com/reference/sstream/istringstream/istringstream/
http://www.cplusplus.com/reference/string/string/getline/
Thanks for the reply. I already found out how to read and search from the text file and display it to the console. I made a maximum tries for the search function which is 3 times. I used a while loop and break the loop if the program found what its searching. The only problem now is that when the program found what its searching, it doesnt break off the loop. Can anyone see what I did wrong?

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	ifstream inFile;
	string NRIC, Name, DOB, Nationality, id, ward;
	int i=0;
	inFile.open("patients.txt");
	if(!inFile)
		cout<<"unable to open"<<endl;
	else
	{
		cout<<left<<fixed;
		
		while(!inFile.eof())
		{
			inFile>>Name>>NRIC>>DOB>>Nationality>>ward;
			if(inFile.fail())
				break;
			else
				
			{   
				

				while(i<3)
				
				{
					cout<<"Please enter the patient's NRIC: ";
				cin>>id;
				system("cls");
				
				if(id==NRIC)
				{
						 {cout<<"This is the patient's information"<<endl;
						  cout<<setw(15)<<"Name"<<setw(15)<<"NRIC"<<setw(15)<<"Date of Birth"<<setw(15)<<"Nationality"<<setw(20)<<"Ward No.";
						  cout<<left<<setw(15)<<Name<<setw(15)<<id<<setw(15)<<DOB<<setw(15)<<Nationality<<setw(15)<<ward<<endl;}

				break;
				}
				
				
				else
				{
					cout<<"No patient associated with that NRIC found"<<endl;
					i++;
				}
				
					}
		}
	}
	
	}
	if (i==3)
	{
						cout<<"You have reached maximum number of trials."<<endl;
				cout<<"Please seek the staff member for enquires."<<endl;
	}
	
	inFile.close();
	system("pause");
	return 0;
}
A couple of thoughts well, quite a number really) come to mind.

First, reading the file.
Instead of looping on eof() which is not recommended, you could change this:
18
19
20
21
22
        while(!inFile.eof())
        {
            inFile>>Name>>NRIC>>DOB>>Nationality>>ward;
            if(inFile.fail())
                break;

to this:
this:
18
19
        while (inFile>>Name>>NRIC>>DOB>>Nationality>>ward)
        {

At this point I have a question. You seem to be reading five text fields from the file, while the sample data contained just three fields (NAME, ID and D.O.B). Something doesn't make sense. Perhaps you have a different text file - a sample would be useful.

I would think the part where the user is asked to input the patient's NRIC should be placed at the top, that is before the start of the while loop which reads the file. Otherwise, it isn't really doing a search, it's just testing a single record.


Yeah I just added some stuffs on the text file. Its 5 fields now. I've posted the text file below. I still don't understand why it still doesnt break the loop :( I want the program to end after it displays the information







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Gabriel                  S1234562I     21/12/1995         SINGAPOREAN      A01          
Nicholas Yang        S9123212U     12/12/1998        SINGAPOREAN        B10
Joel Lok                 S8025787I       5/5/1991        SINGAPOREAN        A02
Cynthia Saram       S4352145N      19/6/1995      SINGAPOREAN        A09
Rebecca Kwok        S7614346N       16/11/1976      SINGAPOREAN        C05
Nicole Chee            S7312948D      23/10/1992      SINGAPOREAN          C08
Henry                   G9597983R       15/09/1990        REPBULIC OF CHINA A05	
Justen Aw             G9592321T       12/5/2000         SINGAPOREAN     B01
Hanna Choo          S8275095J       5/4/1970        SINGAPOREAN       A08
Thaqif Othman     S7712840T      1/12/1989        SINGAPOREAN        C06	
Imran Hazirah     S3306682W     2/5/1983        MALAYSIAN       B06
Spence               S3334682W       3/1/1985      SINGAPOREAN       C03
Amira                 S8732649Y       24/2/1996      SINGAPOREAN    A03
Ahmed Best        T9823798U         27/7/1999      SINGAPOREAN     A07
Lauren Yoo         S8963447O        14/7/1976        SINGAPOREAN        B07
Chewbacca        G0837321P         11/1/1991        JAMAICAN     C07
Shaziri              S9312331H     10/12/1983        SINGAPOREAN      A04
Qasimy              S3894324P    31/7/1983        SINGAPOREAN     B04
I want the program to end after it displays the information

One of the issues is that you have two while loops, one nested inside the other. The break statement only exits from the inner loop while(i<3) but the outer while loop will continue.
As I said previously, It would make more sense if the user input of the NRIC was moved outside of the loop, and done before starting to read from the file. If you want to allow multiple searches, consider either re-arranging the loops so that the file is read from the beginning each time, or perhaps store the data in a vector or array and then search that.


But first there are a few other things to be sorted out.

This line
 
inFile>>Name>>NRIC>>DOB>>Nationality>>ward;

is not going to work reliably with your file.

Let's take a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    string NRIC, Name, DOB, Nationality, id, ward;
	
    ifstream inFile("patients.txt");
    
    while (inFile>>Name>>NRIC>>DOB>>Nationality>>ward)
    {
        cout << setw(20) << Name
             << setw(20) << NRIC 
             << setw(20) << DOB 
             << setw(20) << Nationality 
             << setw(20) << ward 
             << '\n'; 
    }

}

Output:
             Gabriel           S1234562I          21/12/1995         SINGAPOREAN                 A01
            Nicholas                Yang           S9123212U          12/12/1998         SINGAPOREAN
                 B10                Joel                 Lok           S8025787I            5/5/1991
         SINGAPOREAN                 A02             Cynthia               Saram           S4352145N
           19/6/1995         SINGAPOREAN                 A09             Rebecca                Kwok
           S7614346N          16/11/1976         SINGAPOREAN                 C05              Nicole
                Chee           S7312948D          23/10/1992         SINGAPOREAN                 C08
               Henry           G9597983R          15/09/1990            REPBULIC                  OF
               CHINA                 A05              Justen                  Aw           G9592321T
           12/5/2000         SINGAPOREAN                 B01               Hanna                Choo
           S8275095J            5/4/1970         SINGAPOREAN                 A08              Thaqif
              Othman           S7712840T           1/12/1989         SINGAPOREAN                 C06
               Imran             Hazirah           S3306682W            2/5/1983           MALAYSIAN
                 B06              Spence           S3334682W            3/1/1985         SINGAPOREAN
                 C03               Amira           S8732649Y           24/2/1996         SINGAPOREAN
                 A03               Ahmed                Best           T9823798U           27/7/1999
         SINGAPOREAN                 A07              Lauren                 Yoo           S8963447O
           14/7/1976         SINGAPOREAN                 B07           Chewbacca           G0837321P
           11/1/1991            JAMAICAN                 C07             Shaziri           S9312331H
          10/12/1983         SINGAPOREAN                 A04              Qasimy           S3894324P


Notice how everything gets placed into the wrong columns. So the first thing to do is to fix that.

Last edited on
Topic archived. No new replies allowed.