Sorting integers in a text file.

I have programmed a quiz that asks the user their name and stores their final score out of 5 on a text file, so it looks a bit like this:

Abraham scored 4/5!
Bob scored 3/5!
Charlie scored 5/5!
Donny scored 4/5!
Emma scored 5/5!

and the list goes on. There isn't a limit to how many students and their scores may be in the text file. What I would like to do is sort this text file and output the overall results in descending order to the console. Help would be appreciated!

By the way, the text file doesn't have to be altered. Just the correct order must be printed to the console. I'm using C++

Thanks!
closed account (48T7M4Gy)
1. Use and STL container of some sort, vector, array, map, list
2. Read the data into the container from the file
3. Apply the relevant sort function in the required order, keeping in mind descending is just the reverse of ascending
4. Output from container as required

http://www.cplusplus.com/reference/stl/?kw=stl

Cheers :)

PS you can use c-style arrays if you like or aren't allowed to use alternatives or you don'ty use stuff until you learn it, but you have a lot of work ahead of you and it's a whole different ball game. You're looking at key words like bubblesort, mergesort, binary trees etc Good luck with that, I say. :)
Hi, thanks for the reply. But I don't understand this very well since I'm new to C++. Could you give an example of the sort command and how it may be used with a vector? Thanks again.
closed account (48T7M4Gy)
Try this tutorial here as a starting point. You'll see it has a working demo. Vectors are also a good start to STL containers. :)

http://www.cplusplus.com/reference/algorithm/sort

BTW Most if not all courses introduce STL mid way after the first semester or thereabouts so there is no harm in being a bit ahead of the game.

Last edited on
Topic archived. No new replies allowed.