Accepts and write top scores of game user.

/
//My biggest goal is to read the details from the user and write to the file and also be able to use them also to print them and ensure that the ranking is sorted based on the score
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//Here is the Class definition

// Created by Sithe on 6/2/2021.
//

#ifndef COMP315FINALPROJECT_HIGHSCORE_H
#define COMP315FINALPROJECT_HIGHSCORE_H

struct HighScore {
public:
    std::string Name;
    std::string LastName;
    double Score;
    int rank;

    void writeToFile(HighScore *highScore);
    void addNewHighScore(HighScore *high_scores, std::string, std::string, int) const;
    static void addFirst(HighScore high_scores, std::string name, std::string, int score);
    void print_score(std::ifstream& f);
};

#endif //COMP315FINALPROJECT_HIGHSCORE_H

//

// Created by Sithe on 6/2/2021.
//
#include <iostream>
#include <string>
#include <fstream>
#include "HighScore.h"
#include "SortClass.h"
//Used to Print the 5 users with Highest Score
SortClass<HighScore> HighScoreSort;
void  HighScore::print_score(std::ifstream& file) {
    std::string players;
    while (getline(file, players)) {
        std::cout << players;
    }
}


void HighScore::addFirst(HighScore high_scores, std::string name, std::string lastname, int score){
    std::cout<<score;
    high_scores.Name=name;
    std::cout<<score;
    high_scores.LastName=lastname;
    high_scores.Score=score;
    std::cout<<"score";
}

void HighScore::addNewHighScore( HighScore *high_scores, std::string name, std::string lastname, int score) const {
    int size = sizeof(high_scores) / sizeof(high_scores[0]);
    if(size<5){
        high_scores[size].addNewHighScore(high_scores, name, lastname, score);
    }
    for (int i = 0; i < 5; ++i) {
        if(high_scores[i].rank>rank){
            HighScoreSort.sort(high_scores, 5);
            high_scores[0].addNewHighScore(high_scores, name, lastname, score);
        }
    }
}

void HighScore::writeToFile(HighScore *high_scores){
    std::ofstream outputFile;
    outputFile.open("users.txt", std::ios::app);
    int size = sizeof(high_scores) / sizeof(high_scores[0]);
    for (int i = 0; i <size; ++i) {
        outputFile <<(i+1)<< high_scores->Name << " " << high_scores->LastName << " " <<high_scores->Score  <<std::endl;
    }
}

int main(){
    HighScore e;
    HighScore *high_scores;
    HighScore high_scores1;
    std::string Name, LastName;
    int Score;
    std::cout << "Enter the Firt Name : ";
    std::cin >> Name;
    std::cout << "Enter the Last Name : ";
    std::cin >> LastName;
    std::cout << "Enter the rank : ";
    std::cin >> Score;
    e.addFirst(high_scores1, Name, LastName, Score);
    std::cout<<"Hey";

    int size = sizeof(high_scores) / sizeof(high_scores[0]);
    std::cout<<size;
    e.writeToFile(high_scores);
    std::ifstream ifs("user.txt");
    if (!ifs)
        return (std::cout << "Cannot open file\n"), 1;

    e.print_score(ifs);

}


// THe template designed to sort.  
// Created by Sithe on 6/2/2021.
//

#include "SortClass.h"
template<typename T>
class SortClass {
public:
    void sort(T arr[], int SIZE);
};
template <class T>
void sort(T arr[], int SIZE){
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = i+1; j < SIZE; j++)
        {
            if (arr[i] > arr[j])
            {
                T temp;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}


undefined reference to `SortClass<HighScore>::sort(HighScore*, int)'
collect2.exe: error: ld returned 1 exit status


Last edited on
The definition of the template must be available (visible) to the compiler at the point of instantiation; move the template definition to the header file SortClass.h

More information:
https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct high_score {

    std::string first_name ;
    std::string last_name ;
    int score = 0 ;

    // write high score to an output stream (stdout, file etc)
    friend std::ostream& operator<< ( std::ostream& stm, const high_score& hs ) {

        return stm << hs.first_name << ' ' << hs.last_name << ' ' << hs.score ;
    }
};

using high_score_list = std::vector<high_score> ;

// add a high score to the vector
void add_high_score( high_score_list& hs_list, const std::string& first_name,
                     const std::string& last_name, int score ) {

    hs_list.push_back( { first_name, last_name, score } ) ;
}

// sort the vector in descending order of scores
void do_sort( high_score_list& hs_list ) {

    // use standard sort with a predicate to compare the score values
    // https://www.cplusplus.com/articles/NhA0RXSz/
    std::sort( std::begin(hs_list), std::end(hs_list),
               []( const high_score& a, const high_score& b ) { return a.score > b.score ; } ) ;
}

// print the high score list with rank
void do_print( const high_score_list& hs_list, std::ostream& stm = std::cout ) {

     auto hs_list_cpy = hs_list ; // make a copy
     do_sort(hs_list_cpy) ; // sort the copy
     // print each item along with its rank
     for( std::size_t i = 0 ; i < hs_list_cpy.size() ; ++i ) {

        const auto rank = i+1 ; // index i is zero-based, but our rank is one-based
        stm << rank << ". " << hs_list_cpy[i] << '\n' ;
     }
}

int main() { // minimal test driver

    high_score_list scores ;
    add_high_score( scores, "abc", "def", 22 ) ;
    add_high_score( scores, "ghi", "jkl", 56 ) ;
    add_high_score( scores, "mno", "pqr", 81 ) ;
    add_high_score( scores, "stu", "vwx", 37 ) ;
    do_print(scores) ;

    std::cout << '\n' ;
    add_high_score( scores, "yyy", "zzz", 74 ) ;
    do_print(scores) ;
}

http://coliru.stacked-crooked.com/a/a872ff253584f97f
High scores is also part of the OP's other thread http://www.cplusplus.com/forum/beginner/278347/2/
Thank you for your reply @JLBorges For all.

I can see how things go about thank you and I was interested in having a write method that will then write the scores to a file then access them every time the game is played.... and check if this new score is worth to be printed in the file if yes remove the lowest and add the new one.

The game is like this below.

http://www.cplusplus.com/forum/beginner/278347/

and @seeplus what is OP's... ?
OP - Original Poster :)
Okay thanks I really did not know I was now trying to get the CS term OP. 👏👏

Last edited on
Once more I tried to understand the friend and operators seems to be much trick at this stage of a beginner.
Not being sure now how do you go about printing the questions now in a file using them as I am only familiar with the method of:

1
2
3
4
5
void writeToFile(int i, const high_score& high_scores){
    std::ofstream outputFile;
    outputFile.open("users.txt", std::ios::app);
    outputFile<<(i+1)<<" " << high_scores.first_name << " " << high_scores.last_name << " " <<high_scores.score  <<std::endl;
} 


When I want to write the Highscore datas into a file named user.txt;
Last edited on
Topic archived. No new replies allowed.