0

im a Student and new to this site. I want to split my txt file with my highscore data back to my Highscore List. The txt file stores my Highscore like name:score My parsing is not working and i dont know why? I just want to split it to name and score again and then put it in my HighscoreList.

If you have any question about the code just ask :)

#include "highscore.h"

highscore::highscore(){
}

struct highscore::Player{
    string spielerName;
    int score;
};

void highscore::writeHighscore(string name, int score ,int playerNumberx){

    Player HighscoreListe[100];
    for(int i=0;i<=99;i++){
    HighscoreListe[i].score = {0};
    }
    for(int i=0;i<=99;i++){
    HighscoreListe[i].spielerName = "leer";
    }

    HighscoreListe[playerNumberx].spielerName = name;
    HighscoreListe[playerNumberx].score = score;
    int i, j,temp;
    string temp1;
    ifstream myfile("scores.txt");
    string line;


    //heres the point where i need help!!
    if (myfile.is_open()){
        int z=0;
        while(getline(myfile, line)){
            string name1;
            string score1;
            int d = 20;
        while(line[z] != ':'){    
            name1 += line[z];
                z++;
            }
            z = z+2;
            while(line[z] != '\0'){
                score1 += line[z];
                z++;
            }
            HighscoreListe[d].spielerName = name;
            HighscoreListe[d].score = score;
            d++;
        }
    myfile.close();
    }else cout << "Unable to open file" << endl;

    for(i = 0; i<100; i++) {
        for(j = i+1; j<100; j++)
        {
            if(HighscoreListe[j].score < HighscoreListe[i].score) {
                temp = HighscoreListe[i].score;
                temp1 = HighscoreListe[i].spielerName;
                HighscoreListe[i].score = HighscoreListe[j].score;
                HighscoreListe[i].spielerName = HighscoreListe[j].spielerName;
                HighscoreListe[j].score = temp;
                HighscoreListe[j].spielerName = temp1;
            }
        }
    }

    ofstream myfilex("scores.txt");
    if (myfilex.is_open()){
        for(int i = 99;i>89;i--){
            myfilex <<  HighscoreListe[i].spielerName << ":" << HighscoreListe[i].score<<endl;
        }
        myfilex.close();
    }
    else cout << "Unable to open file" << endl;
}


void highscore::readHighscore(){
    string line;

    ifstream myfile("scores.txt");
    if (myfile.is_open()){
        while(getline(myfile, line)){
            cout << line << endl;
        }
    }
    else cout << "Unable to open file" << endl;
}
  • The shown code parses the numerical score, as a text string, into the `score1` `std::string`. There's nothing in the shown code that actually converts it to an integer value. You need to learn The Golden Rule Of Computer Programming: your computer always does exactly what you tell it to do instead of what you want it to do. You did not tell your computer to parse the string into an integer value and save it, so your computer simply does not do that. If you want it to do that, tell your computer ***exactly*** what your computer should do, in this respect. – Sam Varshavchik Jan 24 '21 at 22:45

1 Answers1

0

Make a >> overload for highscore::Player.

In the >> overload

  1. Use std::getline to read a line from the input stream.
  2. Create a std::istringstream out of the line.
  3. Use std::getline to read up to the : from the istringstream into a local string name;.
  4. Use another std::getline to read the rest of the line into a string.
  5. Convert the string into an int with std::stoi and store into a local int score;. Make sure you provide a pos argument.
  6. Ensure that the entire string was converted by comparting the pos argument with the string's length.
  7. If nothing went wrong, store name and score into the highscore::Player passed by the caller. Otherwise, set the failbit on the input stream with setstate
  8. return the input stream.

Now the reading code should be something simple like

int scorecount = 0;
while (myfile >> HighscoreListe[scorecount])
{
    scorecount++;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54