Why is this code crashing ? and how do I put a limit of 100 questions to be outputted ?


Why is this code crashing ? and how do I put a limit of 100 questions to be outputted but not for the test to end ?

Also this doesn't seem to be randomizing at all ?


#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>


using namespace std;

int main()
{



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

questions.push_back("Question 1. What it \"blue\"?\n\n1) A fruit\n2)A color\n3) A drink");
answers.push_back("2");






questions.push_back("Question 2. What is a Ferrari?\n\n1) A car\n2) An animal\n3)A computer");
answers.push_back("1");

srand((unsigned)time(0));

int question_number = rand() % 37;questions.size() ;

string input = "placeholder";

while (input != "quit")
{
cout << questions[question_number] << endl;
cin >> input;
if (input == answers[question_number])
cout << "CORRECT!" << endl;
else
cout << "INCORRECT!" << endl;

question_number = rand()%questions.size();
}

return 0;
}
Last edited on
Out of bounds access in vector questions.

1
2
3
4
5
6
7
questions.push_back("Question 1. What it \"blue\"?\n\n1) A fruit\n2)A color\n3) A drink");
questions.push_back("Question 2. What is a Ferrari?\n\n1) A car\n2) An animal\n3)A computer");
//...
int question_number = rand() % 37;
//...
    cout << questions[question_number] << endl;
Thanks, how would I fix this ?
int question_number = rand() % 37;

replace 37 with questions.size()
Why is this code crashing ?


1
2
3
int question_number = rand() % 37;questions.size() ; // what are u doing here

cout << questions[question_number] << endl; // might be out of bounds 



and how do I put a limit of 100 questions to be outputted but not for the test to end ?

Use a for loop

Also this doesn't seem to be randomizing at all ?

Uhmm, it has only 2 questions.
Btw, you can simply use std::random_shuffle() to shuffle elements in a container.
( if u use rand() u'll have to painfully check if the number returned is not returned already )

And You really need to use structures to organize you data.

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

struct Question {
    string question;
    string options[3];
    char answer;
};

bool isVowel( char c ) { return (c=tolower(c)) == 'a' || c == 'e' || c == 'i' || c =='o' || c == 'u'; }

int main()
{
    vector<Question> questions =
    {
        { "What is blue", { "Fruit", "Color", "Drink" }, '2' },
        { "What is a Ferrari", { "Car", "Animal", "Computer" }, '1' },
        { "What is Programming", { "Art", "...", "None of The Above" }, '1' } 
        // ...
    };
 
    random_shuffle( questions.begin(), questions.end() );
    
    string input = "placeholder";
    
    for( size_t i = 0; i < questions.size(); ++i )
    {
        cout << "Question " << i + 1 << " " << questions[i].question << '\n';
        
        for( size_t j = 0; j < 3; ++j ) // print the options
            cout << j+1 << ") " << (isVowel(questions[i].options[j][0]) ? "An " : "A ") << questions[i].options[j] << '\n';   
    
        cout << endl;
        // ...
    }
    
    return 0;
}

http://coliru.stacked-crooked.com/a/b22e60afd1eb959f

http://www.learncpp.com/cpp-tutorial/47-structs/
Last edited on
Thanks all for the help :)

@ nvrmnd thanks a lot for the detailed reply I will try this out, obviously
you are well advanced because you know what your talking about
you make it look so easy lol.

I usually have better structure when I am programming a full program
but it could be better in general.


Most of the code is new to me, I have written full tests including random
output of answer order, timer, app to txt, and linking user input to the internet as well as allowing the user to change the color of the cmd by number choice.

There is still loads to learn.

Thanks again, I will let you know how I got on.
Last edited on
@ nvrmnd

error: in C++98 'questions' must be initialized by constructor, not by '{...}'

error: deducing from brace-enclosed initializer list requires #include <initializer_list>

23:5: error: deducing from brace-enclosed initializer list requires #include
<initializer_list>

warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]

23:5: error: could not convert '{{"What is blue", {"Fruit", "Color", "Drink"}, '2'}, {"What is a Ferrari", {"Car", "Animal", "Computer"}, '1'}, {"What is
Programming", {"Art", "....", "None of The Above"}, '1'}}' from '<brace-enclose
d initializer list>' to 'std::vector<Question>'

This is what I get when I try to test the code ?????????????
@ rich1


int question_number = rand() % questions.size();questions.size() ;

this is working thanks :) do I need to have questions.size() ; twice ?
no problem :)
no, just once
@Cbasic88

You need to enable C++11 support in your compiler or IDE
@ nvrmnd how I do that ?

What does C++11 mean ? I know what C++ means but what does the number
after it mean ? and why is this not enabled by default?

Thanks again.
@Cbasic88: c++11 is almost like a whole new language. its the revised edition of c++ (i think 09). there is also one coming up called c++14. if you are using gcc or clang, you need compile like this: $(compiler) -std=c++11 (or c++0x) [other flags]. if youre using an ide then there is probably an option to let you do it
Topic archived. No new replies allowed.