So this is my code up until now. I want to put in a search option however I don't know what type of search is the most efficient and no clue how to implement. Can someone point me in the right direction.
Not a comment on the code, but on the design.
In real addresses, DoorNumber may be the name of a house or building, thus it should probably be a string. But for an example program it probably doesn't matter.
None of your fields appear to be unique, although it's possible that a combination of fields (post code, road name, door number) should be unique.
You have a number of choices.
1) With only 10 entries, a simple linear search will work just fine.
2) Use the STL <map> container. Since you don't have a unique field, you would have to write your own comparison routine.
Given the code you've posted so far, you need a way to track whether a slot has been used. What happens if you try to view a slot that has not been entered?
It doesn't make sense to have the search function be a method since the class represents a single entry, not a list of them, and you can't search a single entry in any useful way.
Your Details class is a "blue print" for what a single Detail object is.
It isn't really sensible for an object to contain an array of itselves.
I would go back to using the array of Details in main() like you had before.
What you can do is to write the search function as a regular global function, which searches through an array of Detail objects.
Returning to your previous definition of the Detail class:
1 2 3 4 5 6 7 8 9 10 11 12
class Details
{
public:
Details();
string FirstName;
string LastName;
int Age ;
string Email;
int DoorNumber;
string RoadName;
string PostCode;
};
Im going to try that as my code in main is getting rather large as I have loads of different search options now. Thanks again for your help. Need to learn how to save to text file now. Also why is D[] instead of info?
The info[10] array is local to the main(), it is not global. The LastName_Search() function knows nothing about the info[10] array. It is not in scope within the function.
LastName_Search() only knows of the array passed to it as the 1st parameter in the function call. The name is determined by what appears in the function definition, which I chose to be D.
It is OK to use the name info for the array in the LastName_Search(), but I would consider this to be inviting confusion.
Im going to try that as my code in main is getting rather large as I have loads of different search options now.
So, it may be time to split some of that up into functions too.
I don't see anything wrong with the code in your last post. What is the actual error message?
Just been looking at fstream I don't understand do I need to call the method everytime I want to save something to a file ? because I want to be able to save so I can pull up the records from a file? Don't have a clue how to do this.
Also thanks a lot for explaining A lot of people just show me a piece of code with everything already done. And I have no idea whats going on. Your way makes sense so I can use it later.
You're welcome. I'm glad to see you are picking up on all this so well.
A little more about the array naming issue. I thought of a way to show it clearly.
The search() functions you have are written to work with any array of Details that is passed to it.
Suppose you write a program where several arrays of Details are needed because you are categorizing people (for example). You would be able to use each search() function on any array since the name passed doesn't matter.
Example:
1 2 3 4 5 6 7 8 9 10
int main()
{
Details Students[20];
Details Employees[15];
Details Pensioners[50];
//...
LastName_Search(Students,20);// among students
LastName_Search(Employees,15);// among employees
LastName_Search(Pensioners,50);// among pensioners
}
All 3 of these arrays are known as 'info' in the function.
I would advise trying the file I/O on a simpler task first, since this is a first time for you.
Try just reading and writing an array of 10 integers from/to files.
yea I understood what you meant before I should have separate names so I know where its coming from. Because the codes small now it shouldn't matter but when its bigger it will get more complicated.
I know when I say it doesn't seem like I understand but I do xD.
by I/o do you mean in put output if so are there any good tutorials for this? ive read the one on cplusplus I don't really understand it.
I'm not aware of any especially good tutorials on file I/O (input/output), so maybe search off site.
I searched for "text file I/O" on this site and found a thread which gives some basics:
http://www.cplusplus.com/forum/general/78167/#msg421073
The file I/O really doesn't need to be much more complicated than this:
1 2 3 4 5 6 7 8 9 10
ifstream myfile ("example.txt");
if (myfile.is_open())
{
int value;
while (myfile >> value)
{
cout << value << endl;
}
}
else cout << "Unable to open file";
That code is written to read every integer value from "example.txt" and echo the values to the console (cout << value << endl;) so success can be seen.
Your task is not much harder. Generalizing from the above code to read values into an array (passed as info with size given, like in your search functions):
1 2 3 4 5 6 7 8 9 10 11 12
ifstream myfile ("example.txt");
if (myfile.is_open())
{
int i = 0;
while ((i < size) && (myfile >> info[i]))
{
cout << info[i] << " " << endl;
i = i + 1;
}
myfile.close();// close the file when finished reading from it
}
else cout << "Unable to open file";
This code would read values until they run out (there are less than size values in the file) or until i = size, when it just stops reading.
If you get that working for integers then I'll help you generalize it so that it is reading Details objects from the file, instead of integers.
That represents an integer flowing from a file into value, same as cin >> value; reads an integer from the console (user input).
By generalize I mean overloading operator >> for a Details object so that myfile >> info[i] means read a Details object from the file.
The function for operator >> would be written similar to your existing GetData() function, except without the user prompts (no cout statements).