[C++] Scoreboard, Timer, and Graphics

denmax

You can change this now in User CP.
Reaction score
155
@camelCase & s3rius

I'm not sure if it is required or not, but may I ask for you real names, if it's okay with you? Even if you guys only 'directly' involved yourselves with the scoreboard, your teachings of classes, vectors, fstreams, and more, have been applied to other parts of the program. With that, I wanted to credit you guys for your heavy contribution to the project. Again, if that is okay with you.

Also, it seems my scoreboard causes the console to crash. I'm not sure why, so here are the final codes

SCOREBOARD CLASS
JASS:
class Scoreboard{
 
 
public:
//Add player to vector
    void AddPlayer(std::string name, int points){
        Entry e;
e.name = name;
e.score = points;
        m_scoreboard.push_back(e);
    }
 
//Display scoreboard to console
    void Print(string x){
int size = m_scoreboard.size();
if( size > 5){
size = 5;
}
cout <<   "*-------------------------- LEVEL " << x.c_str();
for(int i=0; i < size; ++i){
cout << "\n*----[" << (i+1) << "]-----";
cout &lt;&lt; &quot;\n*--Name:\t&quot; &lt;&lt; m_scoreboard<i>.name;
cout &lt;&lt; &quot;\n*--Score:\t&quot; &lt;&lt; m_scoreboard<i>.score;
}
    }
 
//Save vector to text file
void Save(std::string x) {
string output;
output = &quot;scoreboards/level&quot;;
output.append(x);
output.append(&quot;.txt&quot;);
ofstream HighScore(output.c_str(), ios::trunc);
for(int i=0; i &lt; 6; ++i) {
HighScore &lt;&lt; m_scoreboard<i>.name &lt;&lt; &quot;\n&quot; &lt;&lt; m_scoreboard<i>.score &lt;&lt; &quot;\n&quot;; //\n acts as a delimiter for getline()
}
HighScore.close();
}
 
//Set contents of txt file to vector
void Load(std::string x) {
string line, output;
repeat = 0;
output = &quot;scoreboards/level&quot;;
output.append(x);
output.append(&quot;.txt&quot;);
Entry e;
ifstream HighScore(output.c_str());
while ( repeat &lt; 5 ) {
getline (HighScore, line);
e.name = line;
getline (HighScore, line);
e.score = atoi( line.c_str() );
m_scoreboard.push_back(e);
repeat++;
}
HighScore.close();
}
    
//bubble sort
void Sort() {
for(int x=0; x&lt;m_scoreboard.size(); x++)
{
for(int y=0; y&lt;m_scoreboard.size()-1; y++)
{
                if(m_scoreboard[y].score &lt; m_scoreboard[y+1].score)
                {
                        Entry temporary = m_scoreboard[y+1];
                        m_scoreboard[y+1] = m_scoreboard[y];
                        m_scoreboard[y] = temporary;
                }
}
}
}
 
//wipes the vector clean
void Wipe(){
        for(std::vector&lt;Entry&gt;::iterator i = m_scoreboard.begin(); i != m_scoreboard.end(); ++i){
            m_scoreboard.erase(i);
        }
}
    
//checks if a similar name is present in the vector, then print it
//feature removed due to crashes
void Global(){
for(std::vector&lt;Entry&gt;::iterator i = m_scoreboard.begin(); i != m_scoreboard.end(); ++i){
if( i-&gt;name == playerName ){
cout &lt;&lt; &quot;\n*--Score:\t&quot; &lt;&lt; i-&gt;score;
}
}
}
 
private:
    std::vector&lt;Entry&gt; m_scoreboard;
int repeat;
};
</i></i></i></i>



EXECUTION
JASS:
switch (menuInput) {
case 1:
hs.Load(&quot;1&quot;);
hs.Sort();
hs.Print(&quot;ONE&quot;);
cin.ignore();
break;
case 2:
hs.Load(&quot;2&quot;);
hs.Sort();
hs.Print(&quot;TWO&quot;);
cin.ignore();
break;
case 3:
hs.Load(&quot;3&quot;);
hs.Sort();
hs.Print(&quot;THREE&quot;);
cin.ignore();
break;
case 4:
hs.Load(&quot;4&quot;);
hs.Sort();
hs.Print(&quot;FOUR&quot;);
cin.ignore();
break;
case 5:
hs.Load(&quot;5&quot;);
hs.Sort();
hs.Print(&quot;FIVE&quot;);
cin.ignore();
break;
case 6:
hs.Load(&quot;6&quot;);
hs.Sort();
hs.Print(&quot;SIX&quot;);
cin.ignore();
break;

PS:
hs is a global variable.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
There's no need to distinctively mention me. I'm just a random geek on the interwebs ;)

Regarding the error:
Can you pin point where it's crashing?

How to do basic debugging:
When you left-click to the left of a line of code you can put a breakpoint (http://i.msdn.microsoft.com/dynimg/IC56046.png - the red point at the left border).
When you then start the program the debugger will pause at this breakpoint. Now, everytime you press F10 (it's probably F10, even for VS2006), you execute a single line of code, until you reach the end of your program.

Put a break point at the start of your main() function, run it and step through it via F10. See which line of code is executed when the crash appears.
The current line that is executed should be indicated by a small yellow arrow. That's at least how it looks in newer versions.
 

denmax

You can change this now in User CP.
Reaction score
155
I've found out the issue minutes after I found out that it crashes, and forgot to edit.

I figured that Wipe() caused the issues. If it erased the nth entry, then the entries after that would have moved down to the lower value.

I initially fixed by just setting the points to negative and the name to something no one will ever ever ever use. I'm thinking of just using vector(m_scoreboard.being()).erase on every loop would fix it too, but I'm too late since I already submitted the project.

JASS:
void Wipe(){
    for(std::vector&lt;Entry&gt;::iterator i = m_scoreboard.begin(); i != m_scoreboard.end(); ++i){
        m_scoreboard.erase(m_scoreboard.begin());
    }
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top