Problems with modifying lists

I have a class that I posted here. It's called FileReader. It is supposed to read in a file and generate a list of integers. It may be useful to know that the list is a list of high scores and any time a score is passed the lowest score should be replaced and then the list should be sorted and reversed. Anyway the problem I think I'm having is that when I pass the list a high score through the method called addScoreToList it isn't adding the score. The other option that may be happining is that I'm not outputing the list elements correctly. (I dont think thats it though) . Can anyone see who I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
FileReader()
      {
         file.open("init.txt");
         file.close();
        //checks to see if the initiate file exists
         if(file.fail())
         {
            file.clear(ios::failbit);
            ofstream myfile ("init.txt");
            if (myfile.is_open())
            {
               string playerName;
               cout << "Enter player name: ";
               cin >> playerName;
               myfile << playerName << endl;
               for(int i = 0; i < 4; i++)
               {
                  myfile << "0" << endl;
               }
               myfile.close();
            }
         }
         file.open("init.txt");
         getline(file, name);
         while(!file.eof())
         {
            string line;
            getline(file, line);
            //converts the string "line" into a c string
            //then into an integer then adds it to the list.
            highScores.push_back(atoi(line.c_str()));
         }
        //orders the scores in descending order;
         highScores.sort();
         highScores.reverse();
      }
   
    /**
       	Returns the current list of high scores
       	*/
   public:
      list<int> getScores()
      {
         return highScores;
      }
   
   public:
      void printScores()
      {
         list<int>::iterator it;
         int i = 1;
         for ( it = highScores.begin() ; it != highScores.end(); it++ )
         {
            cout << i << " - " << *it << endl;
            i++;
         }
      }
   
    /** Gets and returns the player name.*/
   public:
      string getName()
      {
         return name;
      }
   
    /**
    ** Will test to see if the score is higher than another score in the list.
    **	If so it will replace the lower score and return true;
    **
    ** @param score is the new score to be tested.
    */
   public:
      bool addScoreToList(int score)
      {
         bool isHigh = 0;
         list<int>::iterator it;
         for ( it=highScores.end() ; it != highScores.begin(); it-- )
         {
            if	(*it < score)
            {
               *it = score;
               isHigh = 1;
               highScores.sort();
               highScores.reverse();
               break;
            }
         }
         return isHigh;
      }
   
    /**
       	Will save current score list to "init.txt"
       	*/
   public:
      void save()
      {
         file.close();
         remove( "init.txt");
         ofstream newFile;
         newFile.open("init.txt");
         newFile << name << endl;
         list<int>::iterator it;
         for ( it=highScores.begin() ; it != highScores.end(); it++)
         {
            newFile << *it << endl;
         }
         newFile.close();
         file.open("init.txt");
      }
   };
OK nevermind solved my own problem. Heres what I changed

1
2
3
4
5
6
7
8
9
10
11
12
public:
      bool addScoreToList(int score)
      {
         if(score > *highScores.end())   {
            highScores.pop_back();
            highScores.push_back(score);
            highScores.sort();
            highScores.reverse();
            return true;
         }
         return false;
      }
Ok I'm changing the list, but instead of me replacing the lowest score w/ the new one. The list is just seeming to get larger and larger. It seems to be happening either when the file is saved or when it's opened.
Last edited on
Topic archived. No new replies allowed.