Hey everyone, this is my first post. I have viewed these forums alot in the past for C++ help and it has helped me out alot, however I have reached a problem myself. I have been doing C++ since last September and until then i was a complete newbie at it.

My task is to import a .txt document, store in an array which i must then search through to display different information. The text document contains information like:

1000
John Smith
330.50

1001
Tony Robbins
234.65

I must be able to do a search by reference number (the top one) and surname (which i think you can guess which one that is)

I also need to be able to add to these arrays and save to the .txt file when a new quote is made.

The furthest i have gotten so far is to be able to add the .txt files line by line. I am not sure how to add these into an array. I also got the new quotes to be written to the text documents but it overwrote the exisiting quotes. I realise this is something to do with pointer position (i think) which i can fix, but i would like to do one step at a time and just get the quotes into an array and then be able to do searches.

I have read alot about vectors and wondering if this is the way to go? Not that im too sure how to use them.

At the moment my code just imports the .txt document line by line and then closes. Any help/advice on getting these into array(s) and how i would be able to search them would be amazing help.

Thank you in advance!

Hey everyone

Hello.

I am not sure how to add these into an array.

If you want to add the data to a static array, you'll need a large chunk of memory declared beforehand, which works, although it takes up chunk of inflexible memory.

Also, when you're in the file reading loop, keep an index which you use to reference each element. The storing should be as simple as:

/* file stream here*/ >> data[index].name;

Or however you read your file.

Vectors would be just the thing for this. You can dynamically add nodes, and they're really easy to use. I'm not going to explain them here, but why not check out this link:
http://www.cprogramming.com/tutorial/stl/vector.html

Hope this helps

>>The furthest i have gotten so far is to be able to add the .txt files line by line.

Sometimes it's easier to read the file field by field rather than line by line. In this case the file consists of a series of records, with each record having 3 lines. The first line contains one field, the second two fields and the third one field. each field is separated from the other by white space and each record is separated from the next by white space. Therefore using the >> operator to separate the fields of any given record and storing them in a struct/class object which is added to the array would be fine.

You could read the file line by line and then parse the second line into separate fields so you could search on it, but it seems easier to read the file field by field.

>>I also got the new quotes to be written to the text documents but it overwrote the exisiting quotes.

If you never want to change any data already in the file, the you can write to the file in the addendum mode which adds to the back of the file rather than the default truncate mode which overwrites the file contents.

Thank you for your replies, much appreciated. Ive managed to now get the file read in line by line and put each line into a different array. So i now have one array of all the quote numbers, 1 array of all the surnames and so on. It's just search through these arrays i need to do now by quote numbers and surname, then bring out all the other info to do with that certain number. I haven't looked at starting that yet so any advice to get me started would be appreciated.

Thanks, Ian

The technique you describe is sometimes called parallel arrays. It should work fine.

Things to remember; = means assignmen, == means equals, you can't compare arrays (which means C style strings, too) with the == operator. You can use strcmp() or similar functions to compare C style strings. However, if you're allowed to use STL strings, then you can use the == operator to compare one string to the other.

Surely now i have 5 different arrays, with 5 different pieces of information in it, i can do a search to find out its place within the array and then use the array place to find the corrosponding information within the other arrays. Like say i search for reference number 1001, found its place is refnum[1] and then display all other information like surname[1] to go with the correct information? Am i thinking along the right lines or is there a much easier way to do this?

And also i need to break up the surname line into surname and initials as the information is like Smith J. I can do this though by breaking at the space and putting the initials in a different array.

Thanks for your help guys, Ian

Member Avatar for iamthwee

Have you used classes before?

Ok parallel arrays might as well behave like a class in your case/problem

But writing a class here will teach you a lot.

I haven't no, nor do i have the faintest what they are! haha.

Sorry for my newbie ways!

Member Avatar for iamthwee

I haven't no, nor do i have the faintest what they are! haha.

Sorry for my newbie ways!

Well this isn't something I, or anyone else for that matter, can explain in a single post.

Your best bet is to get a book on the subject. Check the tutorials there might be some superficial crapola there for you to read.

Well this isn't something I, or anyone else for that matter, can explain in a single post.

Haha yes i wasn't expecting someone to explain it all to me, I will have a look but i'll probably stick with the way its working at the moment for now. I will check it out though.

Thanks for your help guys, the initial question has been resolved now. I haven''t completed the task yet but will report back here if anything else goes wrong!

Ian

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.