I make a game that will ask you question. But the thing is, how can I randomize the questions everytime I open the program? Also, can I assign the questions to string? Thanks.
Put the questions in an array or vector. Then choose a random number within the range of the size of the container and use that to select the question.
rand() returns you a random number between 0 and some large number.
(rand() % 6) returns you a random number between 0 and 5.
srand(some number) "seeds" the generator, you need to call it once in the program before you call rand().
how can I randomize the questions everytime I open the program?
Depending on how is it stored.
if you have a vector of Question objects:
1 2 3 4
std::vector<Question> questions = /*...*/;
std::mt19937 random_engine(std::time(nullptr)); //Better to use random_device, but it does not work for MinGW
std::shuffle(questions.begin(), questions.end(), random_engine);
//now questions in random order
If you have 3 questions. You can use the random function srand() and rand, to make it pick a number between 1 and 3, and then put that random generator number in an integer variable. Then you can do if statements, saying. If the random number is equal to 1, ask this question, if its equal to 2, ask this question, if its equal to 3, ask that question.
oh and include #include <ctime> for the "time". Everytime you run the program x will will be assigned a number between 1-3. depending on what it is, it will ask whatever question you put in the if statements.
Let's assume your questions are strings, you can initialise a vector of them with:
1 2 3 4 5 6
std::vector<std::string> questions =
{
"What is your name?",
"Where were you born?",
"What is the date?"
};
That will generate a vector named questions. You can ask the vector things like it's size, with questions.size(). And you can use that size expression in the rand() example I posted above.
@MiiNiPaa. He hasnt even learned about arrays, I do not think that he wants to use vector what so ever. I think that progarm is way too complicated for what he is asking for. Im fairly certain he is looking for the one I provided.
I suggest to read vector tutorial avaliable in link provided in comment before vector declaration.
I have a strong opinion (shared by C++ creator) that vector should be learned before arrays: arrays are more tricky, less expressive and are generally a "sharp razor" of C/C++
I 100% agree with you @MiiNiPaa. But, if you are studying c++ in a university, more often or not they will always teach you about arrays and not vectors, (to begin with atleast). Sometimes you get assignments where you must use arrays and can not use vectors.
@Ryanjoshiii if that is the case, learning about arrays is very important, and for simple things like this one its really not that hard (will link to a good tutorial at the end here). But if your univerity, allows you to use vectors, or you're learning by yourself, then use them.
Bucky has very nice tutorials on the basics of c++. You can look up the ones for arrays -
Thank you so much guys! You are all awesome :) Somehow, I manage to randomize questions. I used sir @TarikNeaj codes :) I promise to study arrays and vector :)
But theirs another problem, if I randomized questions, I randonmized the choices, how about to check the answers correctly?
But how can I check if the questions are randomized?
The questions are randomized because you wrote code that randomizes them. Are you sure you didint mean something else? Also, please post all the code you have now.
But how can I check if the questions are randomized?
Group question, possible answer and correct answer in structure. http://www.cplusplus.com/doc/tutorial/structures/
This way you will hold question and answer together allowing you to get answer from question itself.
All other alternative approaches are as effective and sensible as rectal tonsillectomy.
Again @MiiNiPaa. He doesnt even know what an array is, I dont think structures are gonna be helpful or even allowed to be used (if he's studying in university that is), but I could be completely wrong.
OP, do you study in university or self learning, what are you allowed to use?
I think you're just communicating what you want in a bit of a confusing way though.
#include <iostream>
#include <conio.h>
#include <ctime>
#include <string>
usingnamespace std;
int main()
{
string quest1;
char a,b,c,d;
srand(time(NULL));
int x = rand() % 3 + 1;
if (x == 1)
{
quest1 = "\nIf the dog died last night. Did the dog died?";
cout<<"\na. Yes b. The dog is immortal";
cout<<"\nc. No d. Just soccery";
}
elseif (x == 2)
{
quest1 = "\nWho painted Mona Lisa?";
}
elseif (x == 3)
{
quest1 = "\nWho is the first man in the moon?";
}
cout<<"\n\n\n\n\nQuestion no.1"<<quest1;
cout<<"Enter your choice: ";
cin>>a;
switch(quest1)
{
case 1:
switch (a)
{
case'a':
{
cout<<"Correct!";
}
case'b':
{
cout<<"Wrong!";
}
case'c':
{
cout<<"Wrong!";
}
case'd':
{
cout<<"Wrong!";
}
}
}
getch();
return 0;
}