SaveLoad.cpp 1.15 KB
Newer Older
Kyle Malaguit's avatar
Kyle Malaguit committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "SaveLoad.h"

bool SaveLoad::sortAscending(Scores score1 , Scores score2){
    return score1.getScore() > score2.getScore();
}

void SaveLoad::sortByScore(){
    sort(scores.begin(), scores.end(), sortAscending);
}

void SaveLoad::pushBackNewScoreAndSort(Scores Score){
    scores.push_back(Score);
    sortByScore();
}

void SaveLoad::displayScores(){
Kyle Malaguit's avatar
Kyle Malaguit committed
17
    cout << "Rank" << "\t\t" << "Name" << "\t\t" << "Score" << endl;
Kyle Malaguit's avatar
Kyle Malaguit committed
18
    
Kyle Malaguit's avatar
Kyle Malaguit committed
19 20 21 22 23 24
    int i = 1;
    for (auto score : scores){
        cout << i++ << "\t\t" << score.getName() << "\t\t" << score.getScore() << endl;
        if(i == 11){
            break;
        }
Kyle Malaguit's avatar
Kyle Malaguit committed
25 26
    }
    
Kyle Malaguit's avatar
Kyle Malaguit committed
27 28 29 30 31
}

void SaveLoad::userInputNameMakeScore(int intScore){
    char userNameInput[3];
    do{
Kyle Malaguit's avatar
Kyle Malaguit committed
32
        cout << "Please enter a 3 character user name\n";
Kyle Malaguit's avatar
Kyle Malaguit committed
33 34 35
        try{
            cin >> userNameInput;
            if(strlen(userNameInput) != 3){
Kyle Malaguit's avatar
Kyle Malaguit committed
36
                throw runtime_error("Invalid User Name input! \n");
Kyle Malaguit's avatar
Kyle Malaguit committed
37 38 39 40 41 42 43 44 45 46
            }
            break;

        }
        catch(runtime_error &ex){
            cout << ex.what() << endl;
        }
            
    }while(true);
    
Kyle Malaguit's avatar
Kyle Malaguit committed
47
    pushBackNewScoreAndSort(Scores(userNameInput, intScore));
Kyle Malaguit's avatar
Kyle Malaguit committed
48 49
    
    
Kyle Malaguit's avatar
Kyle Malaguit committed
50
}