So I created a code to show your high score using ifstream and ofstream to save them to .txt and to read from them. So when you run the program the first time every thing works flawlessly but when I run it a second time, the name of the player will not show beside the score, instead it will be shown at the bottom with a score of a -656656346456 (something like that) where the actual score is where it is supposed to be!
void high_scores(int scoring){
int qq;
//system("pause"); I know it is bad to use this but this is for school so, yea!
//system("CLS");
system("color f1");
cout<<" %%%%%%%%%%%%%%%%%%Congradulations on finishing the game!%%%%%%%%%%%%%%%%%%"<<endl<<endl;
cout<<" ****************************** ******************************"<<endl;
cout<<" ************************ ***********************"<<endl;
cout<<" ******************** High Scores ********************"<<endl;
cout<<" ************************ ***********************"<<endl;
cout<<" ****************************** ******************************"<<endl;
cout<<endl;
cout<<"Your High Score is: "; cout<<high_score_calculate(scoring)<<". It took you: "<<scoring<<" seconds to end the game!"<<endl<<endl;
qq = high_score_calculate(scoring);
//****************************System to cout high scores -- Reading From .txt files*****************************
ifstream High_Score_Names;
ifstream High_Score;
High_Score_Names.open("High_Score_Names.txt");
High_Score.open("High_Score.txt");
//Check For Error
if( High_Score_Names.fail() || High_Score.fail() ){
cerr<<"\nHigh Score File is missing!"<<endl;
exit(1);
}
//Gets All the names.
int counter=0;
string names[100];
while(!High_Score_Names.eof()){
High_Score_Names >> names[counter];
counter++;
}
//Adds current player name to list
name="Monty";
names[counter+1]=name;
//Get all Scores.
int scores[100];
int k=0;
while(!High_Score.eof() ){
High_Score >> scores[k];
k++;
}
int d=k;
//Adds Current Player score to the list.
scores[k+1] = qq;
//Bubble Sort to order the high scores in order.
int hold;
string name_hold;
for(int p=0; p<d+1 ; p++)
for(int u=0; u<d+1 ; u++)
{
if(scores[u]<=scores[u+1]){
hold=scores[u];
scores[u]=scores[u+1];
scores[u+1]=hold;
name_hold = names [u];
names[u]=names[u+1];
names[u+1]=name_hold;
}
}
//Loop to cout all the high scores.
cout<<"High Scorers:"<<endl<<endl;
cout<<"NAME"<<setw(13)<<"SCORE"<<endl;
for(int m=0; m<d ; m++){
cout<<names[m]<<setw(10)<<scores[m]<<endl;
}
High_Score_Names.close();
High_Score.close();
//**********************************Writing to .txt files. ******************************
ofstream High_Score_Names_2;
ofstream High_Score_2;
High_Score_Names_2.open("High_Score_Names.txt");
High_Score_2.open("High_Score.txt");
//Check For Error
if( High_Score_Names_2.fail() || High_Score_2.fail() ){
cerr<<"\nHigh Score File is missing!"<<endl;
exit(1);
}
for(int g=0; g<=k; g++){
High_Score_Names_2 <<names[g]<<endl;
High_Score_2 << scores[g]<<endl;
}
High_Score_Names_2.close();
High_Score_2.close();
//counter=0;
//k=0;
}
Never Mind I GOT IT WOOOOOOOOOT! OMG it feels so nice when the program works! If you where wondering what was wrong:
My mistake was when I was writing back the names and scores to the text file with the new user. The statement at line 100 should read for(int g=0; g<k; g++)