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;
}
}
}
}
| |