0

I have a class which includes a vector if strings so I can have infinite answers (I'm using this to study by essentially making mock tests). However, when I create thingy, it gets mad at me while trying to have values in the vector. I've tried a lot of ways to get it to work, but it can't.

#include <iostream>
#include <vector>

using namespace std;

string input;

class answer {
public:
    vector<string> answers;
};

class qANDa {
public:
    string question;
    answer answers;
    string correct;
};

void askQuestion (qANDa bob) {
    cout << bob.question;
    getline(cin, input);
    input[0] = tolower(input[0]);
    if (input == bob.correct) {
        cout << "Correct!\n";
    } else {
        cout <<"Incorrect. Study more. Loser.\n";
    };
}

vector<qANDa> thingys;

int main(int argc, const char * argv[]) {

    qANDa thingy = {"The correct answer is \"A\". What's the correct answer.", {} "A"}

    askQuestion(thingys.at(0));
}

I've tried putting strings inside the brackets, I've tried using parenthesis inside the bracket, I've put strings inside the parenthesis inside the bracket, but none of it works.

3
  • Have you tried to put answer() instead of the {} empty brackets? Commented Dec 12, 2013 at 0:59
  • I hadn't, that worked. Thanks for the suggestion. If you add it as an answer, I can mark it as correct. Commented Dec 12, 2013 at 1:04
  • Also, is there a way to put in an answer while defining it? Commented Dec 12, 2013 at 1:07

2 Answers 2

1

Your class answer cannot be initialized just from the empty brackets {}, you might give a default constructed rvalue reference though:

qANDa thingy = 
      { "The correct answer is \"A\". What's the correct answer."
      , answer()
      , "A" }

Also note that at the point you're calling

 askQuestion(thingys.at(0));

thingys contains no element. Change that to

 qANDa thingy = 
    { "The correct answer is \"A\". What's the correct answer."
    , answer()
    , "A"};
 thingys.push_back(thingy);
 askQuestion(thingys.at(0));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I'm now having more problems. I've added the line thingy.answers.a.push_back("A"); in order to add a value, and I get this code shows up midway through: if (__n >= size()) this->__throw_out_of_range(); return this->__begin_[__n]; } Do you know what's wrong? I'm guessing something has a size of 0 (_n has a value of 0 I'm pretty sure).
Thanks, when I saw where I screwed up, I truly realized how stupid I am.
Those who are audacious enough to ask, never will be the stupid ones. Those who never ask are the ones left behind dumb.
0

qANDa has three strings, so the initializer can look like {"one", "two", "three"}.

Oh sorry, I didn't see that the middle was of type answer, which is a vector<string>, not just a single string. If it were a single string the above would have worked. Just do e.g.

qANDa thingy = {"The correct answer is \"A\". What's the correct answer.", answer(), "A"};

Note also the added semicolon at the end.


There is a problem with the askQuestion code storing a character in input[0], when there's no guarantee that the global string-variable input has length >= 1.

To fix that, I suggest changing the type of input from std::string to just char.


Using a global variable to communicate a function result is fraught with dangers. Instead, consider using function results. You will find them discussed in your C++ textbook.

1 Comment

I'm not studying for C++ actually, but answer needs to have the ability to be larger than just one character. Although thanks for telling me, I'll either make sure to either add at least one character or force the user to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.