Input to console

How can I output vector to the console. How I understand It shouuld be in main().

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Task {
private:
    size_t num;
    string question;
    string answer[5];
    int correctAnswer;
    int points;
public:
    Task(){};
    Task(size_t _num, string _question, string _answer1, string _answer2, string _answer3, string _answer4, string _answer5, int _correctAnswer, int _points){
        num = _num;
        question = _question;
        answer[0] = _answer1;
        answer[1] = _answer2;
        answer[2] = _answer3;
        answer[3] = _answer4;
        answer[4] = _answer5;
        correctAnswer = _correctAnswer;
        points = _points;
    };
    void setQuestion(string name_question)
    {
        question = name_question;
    }
    void setAnswer(string answer_name1, string answer_name2, string answer_name3, string answer_name4, string answer_name5)
    {
        answer[0] = answer_name1;
        answer[1] = answer_name2;
        answer[2] = answer_name3;
        answer[3] = answer_name4;
        answer[4] = answer_name5;
    }
    string getQuestion() const
    {
        return question;
    }
    size_t getNum() const
    {
        return num;
    }
    int getCorrectAnswer() const
    {
        return correctAnswer;
    }
    string getAnswer() const
    {
        return answer[correctAnswer - 1];
    }
};
class TestContent {
private:

public:
    std::vector<Task> data;
    std::vector<Task> res;
    void createObject(size_t n, string q, string w0, string w1, string w2, string w3, string w4, int ca, int p)
    {
        Task my_data(n,q,w0,w1,w2,w3,w4,ca,p);
        data.push_back(my_data);
    }
    void deleteQuestion(int count_number)
    {
        data.erase(data.begin() + count_number - 1);
    }
    string checkQuestionID(int q_id)
    {
        for(int i {0}; i < data.size(); i++)
        {
            if(q_id == data[i].getNum()) return data[i].getQuestion();
        }
    }
    vector<Task> MergeQuestion( const TestContent& obj)
    {
        for (int i = 0; i < data.size(); ++i) {
            res.push_back(data[i]);
        }
        for (int j = 0; j < obj.data.size(); ++j) {
            if(res[j].getQuestion() != obj.data[j].getQuestion()){
                res.push_back(obj.data[j]);
            }
        }
        return res;
    }
    vector<Task> CrossingQuestion(const TestContent& obj)
    {
        for(int i = 0; i < data.size(); ++i)
        {
            for(int j = 0; j < obj.data.size(); ++j)
            {
                if(obj.data[j].getQuestion() == data[i].getQuestion())
                {
                    res.push_back(data[i]);
                }
            }
        }
        return res;
    }
    vector<Task> DifferenceQuestion(const TestContent& obj)
    {
        for(int i = 0; i < data.size(); ++i)
        {
            for(int j = 0; j < obj.data.size(); ++j)
            {
                if(data[i].getQuestion() != obj.data[j].getQuestion())
                {
                    res.push_back(data[i]);
                }
            }
        }
        return res;
    }
};

template <class T>
void out( Task&) {
for (int i = 0; i < 5; ++i) {
std::cout << i << std::endl;
}
}

int main() {

    Task A (0, "Have you made supper?","already","still","now", "yet", "I do not remember", 4, 2);
    Task B(1, "My friends asked me to go to the cinema, But I said that I …the movie.", "has already been", "had already saw", "had already seen", "already saw","unknown", 3, 5);
    Task C(2, ".....have they been living in Paris? Only a few months", "How long", "How long time", "What time", "For how long", "what", 1, 15);
    Task E(3, "My First Question", "My first answer 0", "My first answer 1", "My first answer 2", "My first answer 3", "My first answer 4",2, 20);
    Task D(4, "My First Question", "My first answer 0", "My first answer 1", "My first answer 2", "My first answer 3", "My first answer 4",2, 20);
    TestContent a, b, c, e, d;
    a.createObject(0, "Have you made supper?","already","still","now", "yet", "I do not remember", 4, 2);
    b.createObject(1, "My friends asked me to go to the cinema, But I said that I …the movie.", "has already been", "had already saw", "had already seen", "already saw","unknown", 3, 5);
    c.createObject(2, ".....have they been living in Paris? Only a few months", "How long", "How long time", "What time", "For how long", "what", 1, 15);
    e.createObject(3, "My First Question", "My first answer 0", "My first answer 1", "My first answer 2", "My first answer 3", "My first answer 4",2, 20);
    d.createObject(4, "My First Question", "My first answer 0", "My first answer 1", "My first answer 2", "My first answer 3", "My first answer 4",2, 20);

    //vector<Task> MergeQuestion;


   // std::cout << MergeQuestion(Task A (0, "Have you made supper?","already","still","now", "yet", "I do not remember", 4, 2));

    //c.DifferenceQuestion(d);
    //e.CrossingQuestion(d);


    return 0;
}
Last edited on
For dealing with struct/class input/output, it's usually easier to create overloads for the << and >> operators for the required data.

eg

1
2
3
4
5
6
7
8
9
10
11
std::ostream& operator<<(std::ostream& os, const Task& tsk)
{
    // extract to stream os the elements from tsk as needed
    return os;
}

std::istream& operator>>(std::istream& is, Task& tsk)
{
    // input into tsk from the is stream
    return is;
}


and used like:

1
2
3
4
5
6
std::vector <Task> vt;

for (Task t; infile >> t; vt.push_back(t));

for (const auto& t : vt)
    std::cout << t << '\n';

Topic archived. No new replies allowed.