Erik Kubica Erik Kubica - 1 month ago 8
C++ Question

c++ vector multiple values

I am new to c++, but have half a year of work experience with Java se/ee7.

I am wondering how to put 3 values into

vector<string>

example:
vector<string, string, string>
or just
vector<string, string>

to avoid of usage of 3 vectors.

vector<string> questions;
vector<string> answers;
vector<string> right_answer;

questions.push_back("Who is the manufacturer of Mustang?");
answers.push_back("1. Porche\n2. Ford \n3. Toyota");
right_answer.push_back("2");

questions.push_back("Who is the manufacturer of Corvette?");
answers.push_back("1. Porche\n2. Ford \n3. Toyota \n4. Chevrolette");
right_answer.push_back("4");


for (int i = 0; i < questions.size(); i++) {
println(questions[i]);
println(answers[i]);

if (readInput() == right_answer[i]) {
println("Good Answer.");
} else {
println("You lost. Do you want to retry? y/n");
if(readInput() == "n"){
break;
}else{
i--;
}
}
}


I want to use something like
questions[i][0] questions[i][1] questions[i][3]
if it is possible.

Answer Source

Why not having a structure like:

struct question_data {
    std::string question;
    std::string answers; // this should probably be a vector<string>
    std::string correct_answer;
};

and then:

std::vector<question_data> questions;
...
for (int i = 0; i < questions.size(); i++) { // I would personally use iterator
    println(questions[i].question);
    println(questions[i].answers);

    if (readInput() == questions[i].correct_answer) ...
}