Question

In: Computer Science

Code in C++ please You are going to write a program for Computer test which will...

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

Solutions

Expert Solution

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.


Related Solutions

Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user for the number of GPAs to enter. The program should then prompt the user to enter the specified number of GPAs. Finally, the program should print out the graduation standing of the students based on their GPAs. Your program should behave like the sample output below. Sample #1: Enter the number of GPAs: 5 GPA #0: 3.97 GPA #1: 3.5 GPA #2: 3.499 GPA...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
in c++ please In this program you are going to have several files to turn in...
in c++ please In this program you are going to have several files to turn in (NOT JUST ONE!!) hangman.h – this is your header file – I will give you a partially complete header file to start with. hangman.cpp – this is your source file that contains your main function functions.cpp – this is your source file that contains all your other functions wordBank.txt – this is the file with words for the game to use.  You should put 10...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements the algorithm given in Example 1 - 3 (Chapter 1), which determines the monthly wages of a salesperson. The instructions for Example 1 - 3have been posted below for your convenience. Example 1 - 3 Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with...
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT