+ 1
Random function
Is it possible in C to use the random function to generate different questions to ask the user, from the different questions functions Iāve made? (Iām trying to do this for the quiz game Iām developing, so when they choose all it will display random questions Iāve already written in the other functions)š
3 Answers
+ 1
I don't know that much about c, but there is a function int rand(), which will choose a random intreger between 0 and 32767. With this information, you could write something like this (it's not an actual c program)
/*create arrays with the questions and the answers at the same index*/
array questions = ['q1','q2'];
array answers = ['a1','a2'];
/*the function*/
int random_question(){
/*random intreger between 0 and (in this case) 3 - 1, so that if rand() chooses 30270, it would output 20, ask me if you don't get it*/
int index = int rand() % (amount of questions - 1);
/*print the question*/
printf(questions[index]);
/*empty variable, not sure if this is possible, otherewise do string ans = "";*/
string ans;
/*ask for the user input*/
fgets("%s",&ans);
/*I actually had to search this one*/
/*then check if the user has it correct, otherwise show the ans*/
if(ans === answers[index]){
printf("Amazing, that's correct");
} else {
printf("Sorry, the answer is: "+answers[index]);
}
}
+ 1
No problem, you're welcome. I found a lot of the stuff on internet, so maybe that might be a tip for the next time
0
thanks Roel, i wasnt sure about the rand function, as i havent come acorss it yet, but wanted to use it instead of a loop, ill try and see if your idea works for my quiz game