I have made a file that contains a list of people.
Now what I wanted to do was that if I enter a number, I will get the Person corresponding to the person.
Here Blank and FullString are two strings. Blank is a carriage return ("\r") while the FullString is a combined string consisting of a number of facts about the person (Age, Height...)
The Facts are separated by a newline and they have a tab before they are written into the file.
So is there a way to enter a number, then have the computer check against every IDNumber until it finds a match?
The way that I would do it, is create a method that reads in the entire log file into a member variable (string). You could use ifstream.
Then have a separate method that parses the individual "Person" entries from the member variable that holding the entire log into a Vector of "Person" objects or structs (which ever is more appropriate).
Then in your search routine, using IDNumber as the search term, iterate through all "Person" objects until the desired one is found.
This method might not be the fastest or most efficient, but I think it is probably the simplest to implement.
Then have a separate method that parses the individual "Person" entries from the member variable that holding the entire log into a Vector of "Person" objects or structs (which ever is more appropriate).
Could you give me some reference about paring and Vectors?
The parse routine will largely depend on the formatting of the file containing the data.
The simplest way (in my opinion), would be to read in the entire source file into a string object, then use string objects methods such as string::find(...), string::substr(...) and string::compare(...), to perform the search and retrieve operations.
i'm a little bit confused about the use of struct.
A struct is a way to group different kinds of data, usually when they have something in common.
For example a Name, Age and TelephoneNumber are different kinds of data, however the commonality in them is that they could belong to a Person, and thus it would make sense they could be stored within a structure called Person. For example:
1 2 3 4 5 6
struct Person
{
string sName;
int nAge;
string sTelNo;
};
oh, i'm don't really know about the template function (a little bit forgot and a little bit lazy to search it :D ). i understand now, it just the template for struct pair it just written in one line. i got it