In: Computer Science
Code in C++ please
You are going to write a program for Computer test which will read
10 multiple choice questions from a file, order them randomly and
provide the test to the user. When the user done the program must
give the user his final score
Answer
Here the C++ code for your question and quiz questions are given below. Please copy the questions and paste in ".txt" file and save it by name "Quiz.txt".
Given code will select the questions randomly from "Quiz.txt" File.
C++ Code
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#define QNO 30
#define totques 10
using namespace std;
void Display()
{
cout<<"\n\n\n\n\n\n\t\t\t
PRESS ENTER TO PLAY ";
cout<<"\t\t\t\t SCORE CARD
WILL BE PRINT AT END \n";
system("pause");
system("cls");
}
// Function to take Answers input
void asked(string option,string option1,string option2,
string option3,string option4,string correct,string
category,ofstream& fp,int *score)
{
cout << "Your Option : ";
cin >> option;
transform(option.begin(), option.end(),
option.begin(), ::toupper);
option.append(")");
fp<<category<<endl;
// Print answer if you enter correct choice
if (option[0] == correct[0])
{
if (option[0] == option1[0])
fp << "
Your entered correct answer : " << option1 <<
endl;
if (option[0] == option2[0])
fp << "
Your entered correct answer : " << option2 <<
endl;
if (option[0] == option3[0])
fp << "
Your entered correct answer : " << option3 <<
endl;
if (option[0] == option4[0])
fp << "
Your entered correct answer : " << option4 <<
endl;
*score += 1; //
count your score
}
else
{
string correctanswer;
if (correct[0] == option1[0])
correctanswer =
option1;
if (correct[0] == option2[0])
correctanswer =
option2;
if (correct[0] == option3[0])
correctanswer =
option3;
if (correct[0] == option4[0])
correctanswer =
option4;
// If you enter wrong answer
Print correct answer
string wronganswer;
if (option[0] == option1[0])
wronganswer =
option1;
if (option[0] == option2[0])
wronganswer =
option2;
if (option[0] == option3[0])
wronganswer =
option3;
if (option[0] == option4[0])
wronganswer =
option4;
fp << " You entered : "
<< wronganswer << endl;
fp << " Correct answer : "
<< correctanswer << endl;
}
fp<<"
-----------------------------------------------------------------------------\n";
}
int main()
{
// Quiz File with questions
ifstream fins("Quiz.txt");
//Output file to store your answers
ofstream output("NewText.txt");
string text;
string option1, option2, option3,
option4,option,correct,category;
int score = 0;
int arr[QNO];
if(fins == NULL)
{
cout<<"\n\n\n \t\t\t Quiz
File Can't Be OPENED \t\t\t \n\n\n";
}
if(output == NULL)
{
cout<<"\n\n\n \t\t\t NewText
File Can't Be CREATED \t\t\t \n\n\n";
}
Display();
for (int i = 0; i < 30; i++)
{
arr[i] = 10 * i + 1;
}
for (int j = 0; j < totques; j++)
{
srand(time(NULL));
int x = rand()%3;
int loc = (3 * j) + x;
for (int i = arr[3*j]; i <
arr[loc]; i++)
{
string
empty;
getline(fins,
empty);
}
for (int i = 1; i <= 10;
i++)
{
getline(fins,
text);
if(i==1)
{
cout<<"\n\n\n\n\n\nQ"<<j+1<<")
"<<text;
output<<"\nQ"<<j+1<<")
"<<text<<endl;
}
if(i==2)
{
output<<text<<endl;
}
if(i==3)
option1 =
text;
if(i==4)
option2 =
text;
if(i==5)
option3 =
text;
if(i==6)
option4 =
text;
if (i>1
&& i<= 7)
cout << text << endl;
if (i ==
8)
correct = text.substr(0, 40);
if (i ==
9)
category = text.substr(0, 40);
}
asked(option, option1, option2,
option3, option4, correct, category,output,&score);
if (arr[loc] + 10 < arr[3 * (j +
1)])
{
for (int i =
arr[loc] + 10; i < arr[3 * (j + 1)]; i++)
{
string empty;
getline(fins, empty);
}
}
system("cls");
}
output<<"\n You enter total "<< score
<<" correct answer out of 10 ";
fins.close();
output.close();
ifstream out("NewText.txt"); // text file for answers
entered by you
string read;
while(getline(out,read))
{
cout<<read<<endl;
}
system("pause");
return 0;
}
Quiz questions:-
Note:- Save these questions in text file by naming it "Quiz.txt"
Who is the first Indian woman to win an Asian Games gold in 400m run?
A) M.L. Valsamma
B) P.T. Usha
C) Kamaljit Sandhu
D) K.Malleshwari
B) P.T. Usha
Category : Sports
The first Indian to cross seven important seas by swimming ?
A) Amrendra Singh
B) Bula Chaudhury
C) Junko Taibei
D) Yuri Gagarin
B) Bula Chaudhury
Category : Sports
Which was the 1st non test playing country to beat India in
an
international match?
A) Canada
B) Zimbabwe
C) Sri Lanka
D) East Africa
C) Sri Lanka
Category : Sports
Who is the english physicist responsible for the "Big Bang Theory"?
A) Albert Einstein
B) Michael Skube
C) George Gamow
D) Roger Penrose
C) George Gamow
Category : General Science
Revolver discovered by : ?
A) Colt
B) Mercafter
C) Bushwell
D) Daimler
A) Colt
Category : General Science
The first hand glider was designed by…?
A) Leonardo DaVinci
B) The Wright brothers
C) Francis Rogallo
D) Galileo
A) Leonardo DaVinci
Category : General Science
The first news paper in the world was started by ?
A) Japan
B) China
C) USA
D) India
B) China
Category : World History
In which country Aztec civilization was originated ?
A) Greece
B) USA
C) Mexico
D) Egypt
C) Mexico
Category : World History
Who is considered as the master of Greek comedy ?
A) Aeschylus
B) Sophocles
C) Aristophanes
D) Philip
C) Aristophanes
Category : World History
Which of the following is not written by Munshi Premchand ?
A) Gaban
B) Godan
C) Manasorovar
D) Guide
D) Guide
Category : Books And Authors
The celebrated Novel 'The Godfather' was authored by : ?
A) John Milton
B) Victor Hugo
C) Mario Puzo
D) Harold Robbins
C) Mario Puzo
Category : Books And Authors
Who is the author of 'Anandmath'
A) Rabindranath Tagore
B) Bankim Chandra Chattopadyaya
C) Sarojini Nayadu
D) Jyotiba Phule
B) Bankim Chandra Chattopadyaya
Category : Books And Authors
Reykjavik is the capital of which country?
A) Jamaica
B) Jordan
C) Iceland
D) Ireland
C) Iceland
Category : Country Capitals
What is the capital of Finland?
A) Conakry
B) Prague
C) Helsinki
D) None of the above
C) Helsinki
Category : Country Capitals
The Capital of Tanzania is
A) Nairobi
B) Lusaka
C) Kampala
D) Dar- es- Salaam
D) Dar- es- Salaam
Category : Country Capitals
Which one of the following is not a true fish?
A) Silver fish
B) Saw fish
C) Hammer fish
D) Sucker fish
D) Sucker fish
Catgory : Animals And Birds
In which of the following kinds of organism is the phenomenon
found
wherein the female kills the male after copulation ?
A) Dragonfly
B) Honeybee
C) Spider
D) Pit viper
B) Honeybee
Catgory : Animals And Birds
Pashmina shawl is made from the hair of : ?
A) Sheep
B) Goat
C) Rabbit
D) Yak
B) Goat
Catgory : Animals And Birds
Helicopter was invented by
A) Drinker
B) Broquet
C) Copernicus
D) Cockrell
B) Broquet
Category : Inventions
The first hand glider was designed by…?
A) Leonardo DaVinci
B) The Wright brothers
C) Francis Rogallo
D) Galileo
A) Leonardo DaVinci
Category : Inventions
Who is the english physicist responsible for the "Big Bang Theory"?
A) Albert Einstein
B) Michael Skube
C) George Gamow
D) Roger Penrose
C) George Gamow
Category : Inventions
Who proposed the Preamble before the Drafting Committee of the Constitution?
A) Jawaharlal Nehru
B) B.R. Ambedkar
C) B.N.Rau
D) Mahatma Gandhi
A) Jawaharlal Nehru
Category : Indian Politics
Who is the guardian of fundamental Rights enumerated in Indian Constitution?
A) Supreme Court
B) Parliament
C) Constitution
D) President
A) Supreme Court
Category : Indian Politics
The first Law Officer of the Government of India is the
A) Union Law Minister
B) Secreatary, Ministry of Law
C) Attorney-General
D) Chief Justice of the Supreme Court
C) Attorney-General
Category : Indian Politics
The purest form of iron is
A) wrought iron
B) steel
C) pig iron
D) nickel steel
A) wrought iron
Category : Chemistry
The M.K.S. system was first introduced by
A) archimedes
B) Eienstein
C) Newton
D) Giorgi
D) Giorgi
Category : Chemistry
'Yellow cake' an item of smuggling across borders
A) crude form of heroin
B) A crude form of cocaine
C) Uranium oxide
D) Unrefined gold
C) Uranium oxide
Category : Chemistry
Fathometer is used to measure
A) Earthquakes
B) Rainfall
C) Ocean depth
D) Sound intensity
C) Ocean depth
Category : General Awareness
“One People, One State, One leader” was the policy of
A) Stalin
B) Hitler
C) Lenin
D) Mussolin
B) Hitler
Category : General Awareness
Golf player Vijay Singh belongs to which country?
A) USA
B) Fiji
C) India
D) UK
B) Fiji
Category : General Awareness
Screenshot of Input:-
Screenshot of Final Output:-
If I'm able to solve your problem then please thumb up it.