c++ - Allways repeating random pick -
so have code here. objective create multiple choice quiz questions coming text file , picked randomly displayed. able multiple choice, problem when generates, question pick , displayed being displayed again. choices coming answers on text file, problem choices being displayed repeatedly in question.
so want know how not display pick question again, , how choices not being displayed repeatedly.
example output
1. month celebrate christmas? a.december b.december c.january d.december //randomizes questionlist vector random_shuffle(questionlist.begin(), questionlist.end()); //goes through every test question for(int = 0; < questionlist.size(); i++){ vector <test> randomanswer; //puts correct answer first randomanswer.push_back(questionlist[i]); //then randomly gets 3 other answers questionlist while(randomanswer.size() < 4) { int random = rand(); if(random != i){ randomanswer.push_back(questionlist[rand() % (questionlist.size() - 1)]); } //shuffle answers random_shuffle(randomanswer.begin(), randomanswer.end()); //print question cout << questionlist[i].getquestion() << ":" << endl; //initialize first choice character 'a' char ch = 'a'; //prints shuffled answers for(int j = 0; j < randomanswer.size(); j++) { cout << ch << ") " << randomanswer[j].getanswer() << endl; //increment 'a' can print 'b' , forth ++ch; } //get users response cout << "\nyour answer: "; cin >> response; //bool data type determine if correct answer found bool iscorrect = false; switch(toupper(response)) { case 'a': if(randomanswer[0].getanswer()==questionlist[i].getanswer()) iscorrect = true; break; case 'b': if(randomanswer[1].getanswer()==questionlist[i].getanswer()) iscorrect = true; break; case 'c': if(randomanswer[2].getanswer()==questionlist[i].getanswer()) iscorrect = true; break; case 'd': if(randomanswer[3].getanswer()==questionlist[i].getanswer()) iscorrect = true; break; default: cout << "\nincorrect input.\n"; } //if answer found print "correct" else "wrong" if(iscorrect) { cout << "\nyou got answer correct!\n"; } else { cout << "\nyou got answer wrong!\n" << "correct answer " << questionlist[i].getanswer() << endl;
your approach incorrect, insert correct answer in list , randomly pick 3 answers. visual example (1 correct):
1 2 3 4 insert 1 randomanswer randomly pick 3 numbers , insert them randomanswer: ex (2, 1, 3) the correct approach take 4 answers , shuffle them. psuedocode
swap(questionlist[i], *(questionlist.end() - 1)); insert questionlist[i] shuffle questionlist.begin() - questionlist.end() - 2 , randomly pick 3
Comments
Post a Comment