I had made a quiz. The quiz stores the scores of the users in a file. Now I wanted to do two things with the file. One, limit the number of lines in the file, because it will increase at a very fast rate if there is no limit.
Second, I wanted to compare the scores saved scores, so as to only keep High scores.
Can some one help me with this.
Just read the data from the file into some container of structures, and then just perform all the operations (sorting, deleting old entries, etc) on that container.
Just read the data from the file into some container of structures, and then just perform all the operations (sorting, deleting old entries, etc) on that container.
I am not sure, but I think by structures, you are meaning data structures?
If so, how will I separate, the Points form the rest of the string?
And then also I don't know how will it limit the number of lines in the file?
#include <fstream>
usingnamespace std;
int main() {
ofstream outFile("some_file.txt"); //opens an output stream to file "some_file.txt"
ifstream inFile("another_file.dat"); //opens an input stream from file "another_file.dat"
//now we can read and write to file
outFile << "Hello, world!\n"; //writes a string to the output stream
int data;
inFile >> data; //reads some data from the input stream, and stores it in data
cout << data << endl; //print the data to the standard output stream (usually the screen)
//we can reuse are streams with new files
outFile.close(); //we close the first stream
outFile.open("new_file.log"); //and open a new stream to file "new_file.log"
outFile << data; //let's print that data to this file too
//it's very important to remember to close our files when we are done with them
outFile.cloes();
inFile.close()
return 0;
}
//Declerations
string name;
float score;
//Structs and fuctions for the quiz.
//The Main program:
int main()
{
//Data for the structs, and calls to functions:
//Adding data to the Highscores file.
string highscores;
string UserName = "User Name: ";
string Score = "\t\tScore: " ;
//
//Ading data to High scores list
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ofstream high_score ("high_score.txt",ios::out|ios::app);
for (int x = 0;x < 14;x++)
{
UserName[x] ^= Key;
high_score << UserName[x];
}
for (longlongint x = 0;x < Name.size();x++)
{
Name[x] ^= Key;
high_score<< Name[x];
}
for (longlongint x = 0;x < 12;x++)
{
Score[x] ^= Key;
high_score << Score[x];
}
for (longlongint x = 0;x < SCORES.size();x++)
{
SCORES[x]^= Key;
high_score << SCORES[x];
}
high_score << "\n";
high_score.close();
}
//Retriving Data
cout << endl << divider << "The Scores of previous users are: " << endl << endl;
ifstream high_scores ("high_score.txt");
while (high_scores.good())
{
getline (high_scores,highscores);
for (longlongint x = 0; x < highscores.size();x++)
{
highscores[x] ^= Key;
cout << highscores[x];
}
cout << endl;
}
user_score.close();
Here, Score is score of the player converted to string. I have used XOR encryption to prevent tampering with the Highscores. divider is also a string that prints 80 hyphens.
So then back to your original post, limiting the number of lines stored in the file should be easy; however, you need to sort your arrays so that storing the best N scores is as easy as storing the first N scores in your array. There is plenty of documentation out there on how to sort arrays, but if you can't figure it out or need help let me know.
to sort a vector simply include <algorithm> and use sort(vector[firstElemToBeSorted],vector[firstElemToBeSorted+NumberOfElementsToBeSorted])
if i am wrong please say so :-?
Now, I have read about sorting on wikipedia, what i didn't understand is how do I specify the criteria according to which the data is to be sorted.
So how do I limit the number of lines in the file?
If you are going to use vectors, each time the game is played, store the users and scores from the file in to vectors on each run of the program.
create vectors
store file data into vectors
play the game
save the scores/names into vectors
sort the vectors use a for( loop ) to write back to the file
So, if you only want 10 high scores in the file:
1 2 3 4
for( int i = 0; i < 10; ++i )
{
//code to write in to file
}
This way, you'll only have the top 10 scores.
Make sure that you use: ios::trunc with the stream. This way, all current data in the file will be deleted before writing to the file again.
ios::trunc - If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.