boost serialization preperation

hello again, I've been working on this piece of code for a while with the help of people on here, and I am now wanting to include boost serialization into this code. I haven't put any of the relevant boost functions into this code yet, because I am still trying to get it to compile before that point. the code in its current form is a Frankenstein of what I've developed with the help of people here, and my pre-existing, significantly lower quality work. I'm trying to make this code so it uses boost to combine the files that are being called upon in this code, combines them into one, and allows edits to that file, such as adding an additional category (in this instance the additional category would be "movies(M)")
I may be wrong but I think the way I am trying to create the class around L16 may be completely wrong, and might be used for hard coding the categories into the program as opposed to calling them from a file. Im pretty lost here so any help fixing this, or advice on what to do instead would be appreciated

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <boost/thread.hpp>
#include <boost/serialization/list.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class answers {
public:
answers() {}
answers(string t, string c, string s, string m) :teams(t), characters(c), countries(s), movies(m) {}
string data_teams(string teamname, int teamnumber) {
    teams[teamnumber] = teamname;
}
friend class boost::serialization::access;
template < class archive >
void serialize(archive& ar,
    const unsigned int version) {
    ar& teams;
    ar& characters;
    ar& countries;
    ar& movies;
}
};
answers data5;


using namespace std;
int points;
void question(const std::string& fname)
//this clears these values to 0 to ensure that the cypher is not disrupted
{
    vector<string> lines;
    ifstream file(fname);

    if (!file) {
        std::cout << "Cannot open file " << fname << '\n';
        return;
//if the any of the files are not present in the folder then this is printed to show the user that something is wrong, and gives them an idea of how it can be fixed
    }

    for (string line; getline(file, line); lines.push_back(line));

    const auto random_number{ rand() % lines.size() };
    const auto word{ lines[random_number] };
    const auto codes{ new int[word.length()] {} };
    //these lines find and identify the lines within a text file so that they can be mixed up and then changed into numbers at the next code block

    for (size_t i = 0; i < word.length(); ++i)
        if (isalpha(static_cast<unsigned char>(word[i]))) {
            codes[i] = std::tolower(static_cast<unsigned char>(word[i])) - 'a' + 1;
            cout << codes[i] << ' ';
            //these lines change the identifed lines with the text file to the substitution cypher, changing each letter into the corresponding number in the alphabet
        }

    string answer;
    cout << "\nPlease input your answer (no capitals, no spaces): ";
    cin >> answer;
    cout << word;
    //this allows the user to input an answer, and after they have inputted an answer it will also show them the correct answer to see how far off they were (either completly wrong, or perhaps the wrong format)
    if (answer == word) {
        cout << "\nCorrect!\n\n";
        points++;
        //if the user gets the answer correct they are told as such and a point is added to their score
    }
    else
        cout << "\nIncorrect :(\n\n";
    //if the answer is incorrect they are told and are able to continue
}

struct Quiz {
    string fname;
    string title;
    string catnum;
    //i used a struct to created the actual quiz part of the game, as this was simpler than using public and private classes
};

int main()
{
    const Quiz qz[]{ {"Premier_League_Teams.txt", "\nPremier League Football Teams!\n", "first"},
        {"Premier_League_Teams.txt", "\nPremier League Football Teams!\n", "second"},
        {"Hobbit_Characters.txt", "\nHobbit Characters\n", "third"},
        {"Hobbit_Characters.txt", "\nHobbit Characters\n", "fourth"},
        {"South_American_Countries.txt", "\nSouth American Countries\n", "final"} };
    //these lines of code identify the text files from the stored folder, and also output the name of the round, as well as its position in the queue

    srand(static_cast<unsigned int>(time(nullptr)));

    cout << "Epic quiz.\n\n\n";
    cout << "The objective of this round is to unscramble the coded word.\n";
    cout << "You will be given the category as the clue\n";
    cout << "and you have to type out what you believe the answer to be, with a capital letter\n\n";
    //introductory text for the user to read

    string Name;
    cout << "But First, Enter your first name: ";
    getline(cin, Name);
    //this will allow the quiz to be personal to each player, displaying their name at key points and also their name next to their score.

    cout << "\nWelcome contestant " << Name << ". Are you ready to begin the quiz?\n";
    cout << "Please type yes to continue (case sensitive): ";
    string respond;
    cin >> respond;
    if (respond != "yes") {
        cout << "Maybe next time!\n";
        return 0;
        //this adds a degree of playability to the game, offering a quit game button, to increase immersion and the user experience
    }
    cout << "Good luck!\n";

    for (const auto& quiz : qz) {
        cout << "The " << quiz.catnum << " category is...\n" << quiz.title << '\n';
        //this tells the user the number of the category we are on, and what the category is
        question(quiz.fname);
        //this loads the question from the quiz data structure

        cout << Name << ", your score is " << points << " out of 5!\n";
        //after each answer is submitted the user is told their current score out of the total amount of questions
    }
}
Last edited on
i think the way i have done it, it would require a string similar to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    string movies[11] = {
      "thor",
      "hulk",
      "luca",
      "superman",
      "coco",
      "jaws",
      "batman",
      "solo",
      "avatar",
      "aladdin",
      "defiance"
    };



but this is not something I want at all, I want to be able to do this using boost
Last edited on
i tried using this on a previous version also


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    try {
        std::ifstream in("testfile.txt");
        boost::archive::text_iarchive ar(in);
        ar >> data5;
    }
    catch (std::exception& ex) {
        std::cerr << ex.what();
  this prevents a program crash by handling the exception
    }

    teams_data()

    std::list<answers> data;
    data.push_back(data5.teams("poo", "pee"));
    data.push_back(data5.("peee", "proo"));

    {
        std::ofstream out("answers.txt");
        boost::archive::text_oarchive arch(out);
        arch << data5;
    }
Last edited on
Topic archived. No new replies allowed.