So I am writing a program that will help me practice for my driving written exam, I want the questions to appear randomly otherwise I would memorize the order for the answers, I know its about random but I dont know how to apply that in my program, here one example of my code.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
void printTitleTest_01()
{
cout << " ______________________________________________________________________________ ";
cout << "| CA DMV Practice Test 1 |";
cout << " ------------------------------------------------------------------------------ \n";
cout << " You need to score 36 points or more to pass the test\n";
}
int startTest_01()
{
int score = 0;
char answerQuestion;
char answerExit = 'y';
// Question 01
// >>
system("cls");
printTitleTest_01();
cout << "\n 1. If you litter on the roadside, you are subjected to a fine of:\n"
<< " a) $600\n"
<< " b) $1500\n"
<< " c) $500\n"
<< " d) $1000\n\n"
<< " Answer: ";
cin >> answerQuestion;
if (answerQuestion == 'd')
{
cout << "\n Correct!\n\n";
score++; //Adds 1 point to score variable
}
elseif (answerQuestion != 'd')
{
cout << "\n Incorrect!\n\n";
cout << "* Do not litter on the roadside. The fine is $1,000, and you may be forced \n"
<< " to pick up what you threw away. Littering convictions show on your driving \n"
<< " record.\n\n";
}
system("pause");
// Question 02
// >>
system("cls");
printTitleTest_01();
cout << "\n 2. If your parked car rolls away and hits another vehicle, you should:\n"
<< " a) sound horn to attract attention\n"
<< " b) report the incident to city police\n"
<< " c) get the other vehicle repaired\n"
<< " d) remove your car and go on your way\n\n"
<< " Answer: ";
cin >> answerQuestion;
if (answerQuestion == 'b')
{
cout << "\n Correct!\n\n";
score++;
}
elseif (answerQuestion != 'b')
{
cout << "\n Incorrect!\n\n";
cout << "* If you hit a parked vehicle or other property, leave a note with your name, \n"
<< " phone number, and address inside or securely attach it to the vehicle or \n"
<< " property you hit. Report the collision to the city police or, in \n"
<< " unincorporated areas, to the CHP.\n\n";
}
system("pause");
/* And so on with 40 questions in total */
// Total Score
// >>
system("cls");
if (score >= 36)
{
cout << "\n\n Congratulations You passed!\n\n";
cout << " Your total score is " << score << endl;
}
elseif (score < 36)
{
cout << "\n\n Test Over You failed!\n\n";
cout << " Your total score is " << score << endl;
}
return 0;
}
Define your 40 questions as strings.
Create a vector of those 40 strings.
Start a loop, perhaps a for-loop.
For each time through the loop:
Get a random number between 1 and 40.
If the random number is valid,
print the string that the random number points to
Prompt the user for the answer.
If the answer is correct, handle it; else
handle it
Vectors are zero-based, so subtract one from your random number (N) to make sure:
0 <= N < vector.size()
When you set up your strings, just do it at the top of your program:
string
Question1 = "Blah",
Question2 = "More blah",
// and so forth
You will want to put your choices in the same string:
Question2 = "2. If your parked car rolls away and hits another vehicle, you should:\n" \
" a) sound horn to attract attention\n" \
" b) report the incident to city police\n" \
" c) get the other vehicle repaired\n" \
" d) remove your car and go on your way\n\n";
Write a function to fill your vector
vector< string > TestQuestions;
TestQuestions.push_back( Question1 );
TestQuestions.push_back( Question2 );
Also, you will want a vector to store the correct answers.
bool askedBefore[40];
// And then do something like,
if (askedBefore[randomNumber])
{
randomNumber = rand() % 40 // Gets a random number between 0 and 39
}
This is just like playing poker.
Do you remember what did you do before starting a poker game?
Exactly, you REPEATEDLY SWAP ONE CARD WITH ANOTHER CARD.
So to solve your problem, you could:
1. Save all your questions in an array or a vector ( a suit of poker )
2. Call std::random_shuffle to reorder the questions ( swap cards repeatedly )
3. Ask the questions from beginning to the end ( pick cards... )
Ok I understood a little bit but seems I will have to continue reading my book to grasp these concepts, so far I have my code like this, and I would like to know where do I add the if/else statement for the correct answer, and do I store it in the vector? if so how?, last thing, how do I even print the vector so it allows me to pick up an answer?
Thanks for the help, programming is challenging but fun.
#include "stdafx.h"
#include <iostream> // std::cout, std::cin
#include <string> // std::string
#include <cstdlib> // system(), std::srand, std::rand
#include <vector> // std::vector
#include <ctime> // std::time
#include <algorithm> // std::random_shuffle
usingnamespace std;
int DMVTest01()
{
system("cls");
cout << " DMV Test 1\n";
cout << " You need to score 36 points or more to pass.\n" << endl;
string
question01 = "\n If you litter on the roadside, you are subjected to a fine of:\n"" a) $600\n"" b) $1500\n"" c) $500\n"" d) $1000\n\n"" Answer: ",
question02 = "\n If your parked car rolls away and hits another vehicle, you should:\n"" a) sound horn to attract attention\n"" b) report the incident to city police\n"" c) get the other vehicle repaired\n"" d) remove your car and go on your way\n\n"" Answer: ",
question03 = "\n The best way to avoid tailgating is following the rule of:\n"" a - six seconds\n"" b - four seconds\n"" c - three seconds\n"" d - two seconds\n";
/* and so on with 40 questions in total */
vector <string> DMVTest01Questions;
DMVTest01Questions.push_back(question01);
DMVTest01Questions.push_back(question02);
DMVTest01Questions.push_back(question03);
//...
srand(static_cast<unsignedint>(time(0)));
std::random_shuffle(DMVTest01Questions.begin(), DMVTest01Questions.end()); //Is this ok?
return 0;
}
int main()
{
char dot = 15;
int choose;
LOOP:
cout << "\n " << dot << " Choose a Test to practice: ";
cin >> choose;
switch (choose)
{
case 1:
DMVTest01();
break;
case 2:
DMVTest02();
break;
case 3:
DMVTest03();
break;
default:
cout << "\n Error: number not identified.\n";
goto LOOP;
break;
}
return 0;
}
have a loop that gets a random number,
make sure it's valid so you can use vector.at( randomNumber ),
get the user's choice,
verify that the user's choice is valid,
mark the score accordingly
You should put your answer letters (a-d) into a different vector -- that way it's easy to test the user's answer for the correct one:
1 2 3 4 5 6
vector< string > DMVTest01Answers;
DMVTest01Answers.push_back( "d" ); // In the same order as the questions!
DMVTest01Answers.push_back( "b" );
And a "correct answer" vector for when they input the wrong answer:
1 2 3 4 5 6
DMVTest01CorrectAnswers.push_back(
"* Do not litter on the roadside. The fine is $1,000, and you may be forced \n" \
"to pick up what you threw away. Littering convictions show on your driving \n" \
" record.\n\n" );