Hey everyone, I just made a post regarding a project I'm working on and I have another question. I'm writing a program that has the user select from 3 different quizzes. So far, I have it so the user can select what quiz they want to take and actually take it. I want to be able to keep track of their score and tell them their percentage at the end of the quiz. Each quiz has 5 questions so each wrong answer would deduct 20% (Or each right answer would give them 20%. Either way works for me.). How would I keep track of their right/wrong answers and then tell them their percentage at the end of the quiz? Any advice is appreciated! (I had to leave out one of my functions so I didn't exceed the character limit.)
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
void geographyQuiz();
void basicMathQuiz();
void randomTriviaQuiz();
int main()
{
string name;
cout << "Hello! Please enter your name: ";
getline(cin, name);
int quizSelection;
cout << "Please make a selection. Enter 1, 2, or 3\n";
cout << "1. Geography Quiz\n";
cout << "2. Basic Math Quiz\n";
cout << "3. Random Trivia Quiz\n";
cin >> quizSelection;
if (quizSelection == 1)
{
geographyQuiz();
}
elseif (quizSelection == 2)
{
basicMathQuiz();
}
elseif (quizSelection == 3)
{
randomTriviaQuiz();
}
system("pause");
return 0;
}
void geographyQuiz()
{
// Intro to test.
cout << "Let's test your geography skills!\n";
cout << "I will ask you five questions.\n";
cout << "Press the number(s) on your keypad that corresponds with your answer,\n";
cout << "then press 'Enter' to select an answer. Good Luck!\n";
cout << "\n\n\n";
cout << "Geography Test" << endl;
cout << "--------------" << endl;
// Question A.
const string questionA = "A. What continent is Estonia in? \n\n";
int answerA;
cout << questionA;
cout << "1) South America\n";
cout << "2) Europe\n";
cout << "3) Asia\n";
cout << "4) North America\n\n";
cin >> answerA;
if (answerA != 2)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question B.
const string messageB = "B. What is the capital of Canada?\n\n";
int answerB;
cout << messageB;
cout << "1) Vancouver, BC\n";
cout << "2) Calgary, AB\n";
cout << "3) Ottawa, ON\n";
cout << "4) Toronto, ON\n\n";
cin >> answerB;
if (answerB != 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question C.
const string messageC = "C. In what country is Cape Town?\n\n";
int answerC;
cout << messageC;
cout << "1) South Africa\n";
cout << "2) China\n";
cout << "3) Hungary\n";
cout << "4) Egypt\n\n";
cin >> answerC;
if (answerC != 1)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question D. Two are correct. Only select one correct answer.
const string messageD = "D. Which of the following cities is in California? (Two are correct)\n\n";
int answerD;
cout << messageD;
cout << "1) Fresno\n";
cout << "2) Indianapolis\n";
cout << "3) Oakland\n";
cout << "4) Chicago\n\n";
cin >> answerD;
if (answerD != 1 && answerD != 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question E. Two are correct. Select both correct answers.
const string messageE = "E. Russia is in two continents. Which two? (Select more than one letter)\n\n";
int answerE1;
int answerE2;
cout << messageE;
cout << "1) North America\n";
cout << "2) Antarctica\n";
cout << "3) Asia\n";
cout << "4) Europe\n\n";
cin >> answerE1;
cin >> answerE2;
if (answerE1 == 3 || answerE1 == 4 && answerE2 == 3 || answerE2 == 4)
{
cout << "That is correct!\n\n";
}
else
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
system("pause");
}
void basicMathQuiz()
{
cout << "Welcome! Let's test your basic math skills!\n\n";
cout << "I will ask you five questions.\n";
cout << "Press the number(s) on your keypad that corresponds with your answer,\n";
cout << "then press 'Enter' to select an answer. Good Luck!\n";
cout << "\n\n\n";
cout << "Basic Math Quiz" << endl;
cout << "---------------" << endl;
// Question A
const string mathMessageA = "A. What is 2+20?\n\n";
int mathAnswerA;
cout << mathMessageA << endl;
cout << "1. 22\n";
cout << "2. 48\n";
cout << "3. 77\n";
cout << "4. 15\n";
cin >> mathAnswerA;
if (mathAnswerA != 1)
{
cout << "That is incorrect! 20% has been deducted from your score.\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question B.
const string mathMessageB = "B. What is 2+2?\n\n";
int mathAnswerB;
cout << mathMessageB << endl;
cout << "1. 22\n";
cout << "2. 48\n";
cout << "3. 4\n";
cout << "4. 15\n";
cin >> mathAnswerB;
if (mathAnswerB != 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question C.
const string mathMessageC = "What is 5-5?\n\n";
int mathAnswerC;
cout << mathMessageC << endl;
cout << "1. 22\n";
cout << "2. 48\n";
cout << "3. 4\n";
cout << "4. 0\n";
cin >> mathAnswerC;
if (mathAnswerC != 4)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question D
const string mathMessageD = "Pick an equation that equals 0. Two could be right.\n\n";
int mathAnswerD;
cout << mathMessageD << endl;
cout << "1. 0x0\n";
cout << "2. 1+8\n";
cout << "3. 75-75\n";
cout << "4. 100+48\n";
cin >> mathAnswerD;
if (mathAnswerD != 1 && mathAnswerD != 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
// Question E.
const string mathMessageE = "Pick the two equations that equal 20.\n\n";
int mathAnswerE1;
int mathAnswerE2;
cout << mathMessageE << endl;
cout << "1. 5x4\n";
cout << "2. 1+8\n";
cout << "3. 20x1\n";
cout << "4. 100+48\n";
cin >> mathAnswerE1;
cin >> mathAnswerE2;
if (mathAnswerE1 == 1 || mathAnswerE1 == 3 && mathAnswerE2 == 1 || mathAnswerE2 == 3)
{
cout << "That is incorrect! 20% has been deducted from your score.\n\n";
}
else
{
cout << "That is correct!\n\n";
}
system("pause");
}
I am pretty new to programming, but might I suggest a global variable that you can use in each of your sub functions. You could utilize it within the if statement where the answers are incorrect. It could output at the end of your main. I am also not an expert in any way when it comes to this, but most posts I have read on here recommend staying away from using the system("pause").
Hey, thanks for your advice. I ended up creating variables for each question and assigning those variables to numbers based on if the question was answered right or wrong. Then, at the end of the quiz, I add the variables together and output the user's percentage. It works for the purposes of my project, although it may not be the best way to do it. And yeah, the system("pause") isn't the best but it works for the purposes of my project. I'm still a beginner but I've heard that once you get more experienced and advanced with C++ the system("pause") becomes very unnecessary. Again, thanks for your advice!