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
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.
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?
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.