I am trying to write a program in c++. I have a file that contains thousands of numeric entries sorted into four columns. The structure of the data looks something like the following:


3 0.2 0.5 6.2
4 0.1 0.6 6.2 //Track 1
2 0.8 0.5 0.6

2 0.6 0.5 0.4
1 0.8 0.4 2.1 //Track 2
2 0.5 0.4 2.4
4 0.6 0.8 2.5

2 0.3 0.1 0.9 //Track 3
8 0.6 0.8 0.7

The data is grouped into tracks where a new track starts after each line break as shown above (in the actual data there are thousands of tracks). I am trying to write a program that will allow the user to enter a track number. The program will then output the entire track. For example

Track Number: 3
output:
2 0.3 0.1 0.9
8 0.6 0.8 0.7

I need to write the program using classes. I am trying to write a class: class tracks that will contain all of the tracks. The user should be able to call on the class to output the desired track. I am not quite sure how to approach this problem. Any suggestions would be greatly appreciated.

Can you be more specific about your question?
Try to do it yourself, and ask us if you get stuck with something.

You can create a class Track, and all the tracks maybe contained into a vector of Tracks, or you can also create a class called TrackCollection which will handle all the tracks and all the operations needed for them.

is that example the exact format of the file? Or are the track numbers that you put in just comments?

If you're just separating the blocks with a line break, you can use getline to read each line into a string, but check each one and start a new block whenever you find a line with nothing on it (size = 0).

Also, if you only need to display the block on request, you don't have to worry about converting the numbers or anything.

If the blocks are in sequential order with no unused numbers then just store then in a vector or array and use the index value as the block number.

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.