quiz program, randomized questions

Hi. I'm kinda new. I'm writing a program which asks random questions. The idea, is that I have a number of questions, and I generate random numbers which determine the questions that get asked, and after all the questions have been asked, or after the user wants to stop, the users score is tallied. I have this all working so far, except the way I have it, the program can ask the same question twice. Here is my code now.

double w=0;
double score=0;
int userguess;
char guess[20];


quiz:
int ran=0;
srand ( time(NULL) );
ran = rand() % 16 + 1;
cout << ran;
if (ran == 1) {
cout << endl << endl << "a=10, b=20, and c=40" << endl;
cout << endl << "What is a*b*c?";
cin >> userguess;
w++;
if (userguess == 8000) cout << endl << "That's correct! Lets try another one." << endl;
if (userguess != 8000) cout << endl << "Sorry, that's incorrect." << endl;
if (userguess == 8000) score++;
if (userguess == 0) break;}


if (ran == 2) {
cout << endl << endl << "a=10, b=20, and c=40" << endl;
cout << endl << "What is (c/a)*b?";
cin >> userguess;
w++;
if (userguess != 80) cout << endl << "I'm sorry, but that's not the correct answer. Let's try another one." << endl;
if (userguess == 80) cout << endl << "Alright! How about a more difficult problem?" << endl;
if (userguess == 80) score++;
if (userguess == 0) break;}


if (ran == 3) {
cout << endl << endl << "a=10, b=20, and c=40" << endl;
cout << endl << "What is (a*b*C) divided by (2*a+b+c)?";
cin >> userguess;
w++;
if (userguess == 100) cout << endl << "Yep!" << endl;
if (userguess != 100) cout << endl << "Nope." << endl;
if (userguess == 100) score++;
if (userguess == 0) break;}


if (ran == 4) {
cout << endl << "Express base 10 numeral, the number 3 in binary?";
cin >> userguess;
w++;
if (userguess == 11) cout << endl << "That's correct! Lets try another one." << endl;
if (userguess == 11)score++;
if (userguess == 0) break;
if (userguess != 11) cout << "Sorry, that's incorrect." << endl;}


if (ran == 5) {
cout << endl << "Name second highest peak on earth.";
cin >> guess;
w++;
if (guess[0] == 'k') cout << endl << "That's correct! Lets try another one." << endl;
if (guess[0] == 'k') score++;
if (guess[0] == 0) break;
if (guess[0] != 'k') cout << "Sorry, that's incorrect." << endl;}


if (ran == 6) {
cout << endl << "The longest living mammal is a/an []?" << endl << endl;
cout << "[a]Whale [b]Human [c]Turtle [d]Elephant" << endl;
cin >> guess;
w++;
if (guess[0] == 'a') cout << endl << "Correct. The Greenland Right whale has a life expectancy of at least 170 years." << endl << ":";
if (guess[0] == 'a') score++;
if (guess[0] == 0) break;
if (guess[0] != 'a') cout << "Sorry, that's incorrect." << endl;}


if (ran == 7) {
cout << endl << "A water molocule consists of how many atoms?";
cin >> userguess;
w++;
if (userguess == 3) cout << endl << "That's correct! Lets try another one." << endl;
if (userguess == 3) score++;
if (userguess == 0) break;
if (userguess != 3)cout << "Sorry, that's incorrect." << endl;}


if (ran == 8) {
cout << endl << "Which of the following is true of superconductors?";
cout << endl << endl << "[a] They are highly attracted to magnets" << endl << "[b] They repel magnetic fields"<<
endl << "[c] They are dangerously radioactive" << endl << "[d] They can function under extremely high temperatures" << endl << endl << ":";
cin >> guess;
w++;
if (guess[0] == 'b') cout << endl << "That's correct! Lets try another one." << endl;
if (guess[0] == 'b') score++;
if (guess[0] == 0) break;
if (guess[0] != 'b') cout << "Sorry, that's incorrect." << endl;}


if (ran == 9) {
cout << endl << "The sound frequency range of human hearing is commonly said to be" << endl << endl <<
"20 hz to [] khz. []=";
cin >> userguess;
w++;
if (userguess == 20) cout << endl << "That's correct! Lets try another one." << endl;
if (userguess == 20) score++;
if (userguess == 0) break;
if (userguess != 20) cout << "Sorry, that's incorrect." << endl;}


if (ran == 10) {
cout << endl << "The planet venus is the X closest planet to the sun. What # does x equal?";
cin >> userguess;
w++;
if (userguess == 2) score ++;
if (userguess != 2) cout << endl << "What planet are you from? Don't answer that." << endl;
if (userguess == 2) cout << endl <<"You are correct!" << endl;
if (userguess == 0) break;}


if (ran == 11) {
cout << endl << "which is the fourth closest planet to the sun?";
cin >> guess;
w++;
if (guess[0] == 'm') cout << endl << "Correct." << endl;
if (guess[0] == 'm') score++;
if (guess[0] == 0) break;
if (guess[0] != 'm') cout << endl << "Wrong answer." << endl;}


if (ran == 12) {
cout << endl <<"The last name of the English who has been credited with developing calculous is?";
cin >> guess;
w++;
if (guess[0] == 'n') cout << endl << "Very Good." << endl;
if (guess[0] == 'n') score++;
if (guess[0] == 0) break;
if (guess[0] != 'n') cout << endl << "Sorry, lets try the next question." << endl;}


if (ran == 13) {
cout << endl << "Which is the first name of the female theorist is considered to have written the first algorithm" << endl << "intended to be processed by a machine?";
cin >> guess;
w++;
if (guess[0] == 'a') cout << endl << "You are correct!" << endl;
if (guess[0] == 'a') score++;
if (guess[0] == 0) break;
if (guess[0] != 'a') cout << endl << "The answer you gave is not the correct answer." << endl;}


if (ran == 14) {
cout << endl << "When multiple discs of equal diameter are laying flat against the same flat";
cout << endl << "surface, how many discs can one disc touch at one time?";
cin >> userguess;
w++;
if (userguess == 6) score++;
if (userguess == 6) cout << endl << "Correct." << endl;
if (userguess != 6) cout << endl << "Nope." << endl;
if (userguess == 0) break;}


if (ran == 15) {
cout << endl << "A female blank, lives in the ocean, but lays it's eggs in fresh water.";
cin >> guess;
w++;
if (guess[0] == 's') cout << endl << "Correct." << endl;
if (guess[0] == 's') score++;
if (guess[0] == 0) break;
if (guess[0] != 's') cout << endl << "That's incorrect." << endl;}


if (ran == 16) {
cout << endl << "The element gold(Au) is heavier than platinum(P)t. true or false?";
cin >> guess;
w++;
if (guess[0] == 't') cout << endl << "Nice work." << endl;
if (guess[0] == 't') score++;
if(guess[0] == 0) break;
if (guess[0] != 't') cout << endl << "That is not the correct answer." << endl;}
if ( w < 16 ) goto quiz;

char*SND="C:\\rg.WAV";
sndPlaySound(SND, SND_ASYNC);

cout << endl << endl << " You have completed the quiz. You correctly answered " << score << " out of " << w << " questions." << endl;
cout << endl << " That calculates to " << (100*score)/w << "%.";
if (((100*score)/w) >= 90) cout << " You get an A.";
else if (((100*score)/w) >= 80 ) cout << " You get a B.";
else if (((100*score)/w) >= 70) cout << " You get a C.";
else if (((100*score)/w) >= 60) cout << " You get a D.";
else cout << " You have failed the quiz.";
cout << endl << endl << endl << " main menu [m] exit [t] " << endl << endl;
cin >> guess[0];
if (guess[0] == 'm') break;
if (guess[0] == 't') return 0;
Last edited on
Maybe add an bool array with elements for each question, then as soon as that question has been asked it turns it's corresponding element to true. After that you'd only need to add it to each if statement.
I think I tried an approach similar to what your describing. I made an int array at the top

int x[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

and in each if statement holding a question, I changed,

if (ran == 1) // to if(ran==x[1]), and after after asking the question, x[1]=0.

This works, but it is very slow. Near the end, the program has to keep generating random numbers between 1 and 16, and checking each statement, until a specific number is generated. I'm trying to figure out a new approach.

Is there a way that I can randomly select a value inside and array, and then redefine that array to disclude values already been generated?

Like something like this, but which works.

srand ( time(NULL) );
ran = rand() %x[];




Last edited on
Topic archived. No new replies allowed.