SaveLoad.cpp 1.11 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
    for (int i = 0; i < 10; i ++){
        
        cout << i+1 << "\t\t" << scores.at(i).getName() << "\t\t" << scores.at(i).getScore() << endl;
Kyle Malaguit's avatar
Kyle Malaguit committed
22 23
    }
    
Kyle Malaguit's avatar
Kyle Malaguit committed
24 25 26 27 28
}

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

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