Write a simple application to conduct a computer based quiz. The quiz consists of questions with answers true or false only. Suppose the questions are present in a text file Questions.txt, in the following format:
First line of the file consists of the number of questions present in the file and the rest of the file consists of the question followed by answer in separate lines. Your program should display questions one by one, and prompt the user to enter his answer, at the end display the score of the user. And also display the questions the user has wrongly answered along with the correct answer. (You can assume that each question is of length at most 80 characters). Use command line arguments to provide the text file to the program.
A sample Questions.txt file:
3
There are one thousand years in a CENTURY.
False
DOZEN is equivalent to 20.
False
The past tense of FIND is FOUND.
True
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
ifstream infile("quizfile.txt");
string line;
int score = 0;
string answer = "";
int count = 0;
while (std::getline(infile, line))
{
cout << line;
cin >> answer;
std::getline(infile, line);
if (answer == line)
{
score++;
}
}
std::cout << score << std::endl;
return 0;
}
Comments
Leave a comment