I have a file that is named (scorelist.txt) and I am not very knowledgeable on arrays. The user will input their name and the name will be searched in the file, and if the name is found in the textfile, the score will be outputted to the screen cout %26lt;%26lt; "Your Highscore is " %26lt;%26lt;score %26lt;%26lt;endl;
But I have no clue how to do this and I really want to know how to do this using arrays.
the textfile will be in this format:
name1
score1
name2
score2 ect..
Any help will be great!
-Randy
C++ Arrays: Sorting and Finding High scores from a outside file?
I think you have to use a vector, since you don't really know the exact number of names.
One method would be to use two vectors; one to store the names and one to store the scores. You need to write a method to read from a text file, this can be done using fstream. As you read each line, pushback that line to either the name or score vector. Keep note that the format of the text file, is name, score, name, score,... So you have to distinguish which lines are names and scores. After putting all of the data into the vectors, search the inputted name in the name vector by looping. When you find a match, store the index value. Now, retrieve the data stored in the score vector using the stored index value.
It's probably not the most elegant method, but it's the method that first popped into my head. Hope this helps!
yu gi oh cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment