Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ documented program to perform the role of a quiz maker. The program

ID: 3709628 • Letter: W

Question

Write a C++ documented program to perform the role of a quiz maker. The program should randomly display True/False questions, record student's answers, keep track of the timing, and calculate the final scores out of 10. The program should work as follows: Prompt the student to enter their First name and Last name (both of type string), and ID (a number of 6 digits) Validate the entered ID, if the length of ID is not 6 digits, stay at step 2 and prompt the user to re-enter the correct ID. After 3 failed attempts, exit the program. If the student entered a valid name and ID go to step 3. Randomly pick ten questions with their answers form the Q and A arrays. Display one question at a time. Get the answer then move to the next question. Do not accept answers other than true or false (T or F should be fine too). The answers should not be case sensitive. When the user is done with the quiz, print out a report (On screen) with the below information in it: First name and Last name Score Elapsed time Student’s answers and the correct answer. Prompt the student to enter Q to exit the program, or to enter S to clear the screen and start a new quiz for another student. Requirements The program skeleton will include two string arrays "Q_Array.txt" for questions and "A_Array.txt" for answers, that you will use to pick the quiz questions from. In each quiz, your program should display ten unique random questions (no repeated questions). Users have 10 minutes to complete the quiz, after 10 minutes the program should automatically terminates the quiz and report the outputs to the screen. All questions have the same weight. 10 questions for 10 points. Code optimization (breaking your code to functions): Modify your code to include the following functions: bool Validate_ID (int ID): to validate the length of the ID, if 6 returns true otherwise returns false. bool Unique_Question (int questionNumber): to check if the question already was used before in the quiz or not, if the question was used before returns false, otherwise returns true. Grading Rubric: Workable Program 35 Using Code Optimization (Functions) 15 Correct output 25 Using descriptive variables names 10 Comments/readability of program 15 Extra Credits: (15 points) By default the quiz maker, will display 10 questions for the student, each question weight 1 point. Give an option for student to choose the number of questions in the quiz, options will be 10 questions or 20 questions before starting the exam: If the student chose 10 questions, then each question weight 1 point If the student chose 20 questions, then each question weight 0.5 point (25 points) Modify your program so instead of having the questions and answers in your code, questions can be read from a text file name it “TestBank.txt” and answers from a text file “Answers.txt”, and dynamically during runtime assign the questions to Q array and answers to A array.

Explanation / Answer

questions.txt

Where does a C++ program begin is execution?
The first function in the file
The first function by alphabetic order of names
The function main( )
The function specified in the #DEFINE FirstFunction


How many values can be returned from a function by means of the "return" statement?
1
2
3
Lots and Lots

Which loop type is guaranteed to execute at least once?
for
while
do...while
all the above

Arrays are passed to functions by:
reference
value
either method
neither method

Global variables should be used...
in place of function parameters
only when absolutely needed
never
only during the full moon

When storing a floating point value to an integer type variable, the fractional part is:
rounded up
rounded down
branched
truncated

answers.txt
C
A

C
A


B
d

main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <ctime>
#include <cstdlib>

using namespace std;


int read_questions(ifstream &, string [][5]);
int read_answers(ifstream &, char []);
int play_game(int , char [], string [][5], string);
void show_question(string [][5], int, char);
char player_try();
void sort_score(string, int);


/*=====================================================================
Function: main
Description: A high level organizer. Check the command line arguments, calls
   the functions to read the input files, check that the questions and answer
   files have the same number of items, call the play_game( ) function, call
   sort_score( ) function.
Parameters: int argc - argument count
           char *argv[] - hold arguments
======================================================================*/
int main(int argc, char *argv[])
{
   if (argc != 3)
       {
           cout << "Command line arguments are incorrect." << endl;
           return -1;
       }

   ifstream fin_q(argv[1]);

   //If questions file does not exist. Terminate.
   if(!fin_q)
   {
       cout << "Questions file failed to open. Program terminated.";
       return -1;
   }

   ifstream fin_ans(argv[2]);

   //If answers file does not exist. Terminate.
   if(!fin_ans)
   {
       cout << "Answers file failed to open. Program terminated.";
       return -1;
   }


   //Create array to hold questions and enter read_questions function
   string questions[50][5];
   int qCount = read_questions(fin_q, questions);

   // If read_questions returns "-1" display error and terminate.
   if(qCount == -1)
   {
       cout << "Error: Questions file empty.";
       return -1;
   }

   // Create array for answers and enter read_answers function
   char answers[50];
   int count2 = read_answers(fin_ans, answers);

   // If # of answers does not equal # of questions display error & terminate.
   if(qCount != count2)
   {
       cout << "Error: Number of answers does not match number of questions. ";
       return -1;
   }


   // Get player name.
   string name;
   cout << "Please enter player name: ";
   cin >> name;

   // Main play_game function, return final_score.
   int final_score = play_game( qCount, answers, questions, name );

   cout << endl << "Final score: " << final_score << endl;


   // Create and open summary.txt.
   string fname = "summary";
   fname+= ".txt";

   ofstream fout(fname.c_str(), ios:: app);

   if(!fout)
       {
           cout << "Cannot write to the target file." << endl;
           return -1;
       }

   //Write name and score to summary.txt
   fout << name << " " << final_score << endl;


   //Enter sort_score function to get top score and player rank.
   sort_score(name, final_score);


   cout << "GG." << endl <<endl;


   // Close all files before exit.
   fin_q.close();
   fin_ans.close();
   fout.close();

   return 0;
}


/*=====================================================================
Function: read_questions
Description: Function reads questions.txt into a 2d array of strings and
   returns the number of questions
Parameters: ifstream &fin_q   - file to read the questions from
           string questions[][5] - array to hold questions
======================================================================*/
int read_questions(ifstream &fin_q, string questions[][5])
{
   int i = 0;       //counts number of questions
   string test;   //temporary string to hold text from file


   // If questions file empty return error to main.
   if(!getline(fin_q,test))
   {
       return -1;
   }

   //While not end of file read questions to array
   while(!fin_q.eof())
   {
       if(test.size() > 1)       //If text detected then begin loop
       {
           for(int j = 0; j < 5; j++)
           {
               questions[i][j] = test;
               getline(fin_q, test);
           }
           i++;
       }

       getline(fin_q,test);
   }

   return i;
}


/*=====================================================================
Function: read_answers
Description: Reads answers.txt into array of chars and returns the number
   of answers.
Parameters: ifstream &fin_ans   - file to read the answers from
           char answers[] - array to hold answers
======================================================================*/
int read_answers(ifstream &fin_ans, char answers[])
{
   int i = 0;       //loops through array and returns # of answers
   char test;       //temporary char to hold text from file

   //while not end of file read file into array answers[]
   while(!fin_ans.eof())
   {
       fin_ans >> test;
       answers[i] = toupper(test);
       i++;
   }

   return i - 1;
}


/*=====================================================================
Function: play_game
Description: Main control of game play. Function picks questions, checks
   correctness of player response, offers second chance, determines
   and applies scoring. Returns final score.
Parameters: int qCount   - number of questions
           char answers[] - array of correct answers
           string questions[][5] - array of questions and choices
           string name - players name
======================================================================*/
int play_game(int qCount, char answers[], string questions[][5], string name )
{
   bool gameON = 0;   //Flag that checks if unanswered questions remain
   bool skip;           //Flag if player has skipped a question
   char choice;       //Contains players choice
   char chance;       //Flag if player has taken second chance
   int qNum = 1;       //Contains the current round number
   int score = 1;       //Contains and modifies player score

   // Create array of bool and initialize to false.
   bool check[50];
   for(int i = 0; i < qCount; i++)
       check[i] = false;


   do
   {
       skip = 0;
       chance = 'N';
       choice = '0';
       gameON = 0;

       // Generate random question not used previously.
       int rand_num;
       srand((unsigned)time(NULL));
       do
       {
       rand_num = rand( ) % qCount;
       }while(check[rand_num] == 1);

       check[rand_num] = 1;

       // Display question.
       cout << endl << name << " here's question number " << qNum;
       show_question(questions, rand_num, '0');

       // Ask for and accept player's first try.
       choice = player_try();

       // If correct answer. Give full points.
       if(choice == (answers[rand_num]))
       {
           score *= 10;
           cout << "Correct! Current score is " << score << endl;
       }
       // Else, wrong answer, ask to skip or second chance.
       else
       {
           cout << "Incorrect. "
                   "Second chance (Y) or (N)? > ";
           cin >> chance;
           chance = toupper(chance);

           // Verify input is correct.
           while(!((chance == 'Y') || (chance == 'N')))
           {
               cout << "Incorrect input. (Y) for second chance or (N) "
                       "to skip. ";
               cin >> chance;
               chance = toupper(chance);
           }

           cout << chance << endl;

           // If select to skip question, keep score unchanged.
           if(chance == 'N')
           {
               cout << "Question skipped. Score is " << score << endl;
           }
           // Else if select second chance, re-display question.
           else if(chance == 'Y')
           {
               cout << endl << "Second chance.";
               show_question(questions, rand_num, choice);
               choice = player_try();

               // If correct answer increase score by half points.
               if(choice == (answers[rand_num]))
               {
                   chance = 'N';
                   score *= 5;
                   cout << "Correct! Score is " << score <<endl;
               }
               // If wrong answer set score to 0 and announce Game Over.
               else
               {
                   score = 0;
                   cout << "Incorrect. GAME OVER!" << endl;
               }
           }

       }

       qNum++;

       // If check array still has unanswered questions, continue game.
       for(int i = 0; i < qCount; i++)
       {
           if(check[i] == 0)
               gameON = 1;
       }

   }while((gameON == 1) && (chance == 'N'));


   return score;

}


/*=====================================================================
Function: show_question
Description: Displays question and its choices to player.
Parameters: string questions[][5] - array of questions and choices
           int rand_num - random question from array
           char choice - previous choice user has made (A,B,C,D or none)
======================================================================*/
void show_question(string questions[][5], int rand_num, char choice)
{
   //Display question.
   cout << endl << questions[rand_num][0] << endl;

   //Display appropriate responses based off previous choice (A-D).
   switch(choice)
   {
   case '0':
           cout << "A. " << questions[rand_num][1] << endl;
           cout << "B. " << questions[rand_num][2] << endl;
           cout << "C. " << questions[rand_num][3] << endl;
           cout << "D. " << questions[rand_num][4] << endl;
           break;

   case 'A':
           cout << "B. " << questions[rand_num][2] << endl;
           cout << "C. " << questions[rand_num][3] << endl;
           cout << "D. " << questions[rand_num][4] << endl;
           break;

   case 'B':
           cout << "A. " << questions[rand_num][1] << endl;
           cout << "C. " << questions[rand_num][3] << endl;
           cout << "D. " << questions[rand_num][4] << endl;
           break;

   case 'C':
           cout << "A. " << questions[rand_num][1] << endl;
           cout << "B. " << questions[rand_num][2] << endl;
           cout << "D. " << questions[rand_num][4] << endl;
           break;

   case 'D':
           cout << "A. " << questions[rand_num][1] << endl;
           cout << "B. " << questions[rand_num][2] << endl;
           cout << "C. " << questions[rand_num][3] << endl;
           break;
   }
}


/*=====================================================================
Function: player_try
Description: get the choice from player, validate response is in range A-D,
           return players choice.
Parameters: none
======================================================================*/
char player_try()
{
   //Ask for and hold player choice
   char choice;
   cout << "Your choice? > ";
   cin >> choice;


   // Verify that choice is in range A-D, else print out warning
   switch(choice)
   {
       case 'a':
       case 'b':
       case 'c':
       case 'd':
                  choice = toupper(choice);
                  break;
       case 'A':
       case 'B':
       case 'C':
       case 'D': break;

       default:
       cout << "Invalid selection. Please enter A, B, C, or D." << endl;
       player_try();
       break;
   }

   return choice;
}


/*=====================================================================
Function: sort_score
Description: read in records of previous players. Sort the scores and names,
   display the current high score and players name, and display the rank of
   the current player based on his/her score.
Parameters: string name - players name
           int final_score - final score of current player
======================================================================*/
void sort_score(string name, int final_score)
{
   int score[100];       //array of arbitrary length to hold scores
   string player[100];   //array of arbitrary length to hold names
   int count = 0;       //counts number of records in summary.txt

   //open and verify summary.txt
   ifstream fin_scores("summary.txt");

   if(!fin_scores)
       cout << "Could not open summary.txt";


   //while not end of file read in names and scores from file to arrays
   while(!fin_scores.eof())
   {
       fin_scores >> player[count];
       fin_scores >> score[count];
       count++;
   }

   int total_records = count-1;   //total # records in summary.txt


   //selection sort records from highest to lowest.
   int startScan, maxIndex, maxValue;
   string maxPlayer;

   for (startScan = 0; startScan < (total_records - 1); startScan++)
   {
       maxIndex = startScan;
       maxValue = score[startScan];

       for(int index = startScan + 1; index < total_records; index++)
       {
          if (score[index] > maxValue)
          {
           maxValue = score[index];
           maxPlayer = player[index];
           maxIndex = index;
          }
       }
       //assign player and score to correct array position
       score[maxIndex] = score[startScan];
       player[maxIndex] = player[startScan];
       score[startScan] = maxValue;
       player[startScan] = maxPlayer;

   }


   //display current high score
   cout << endl << "===================================" << endl;
   cout << "Current high score: " << player[0] << " " << score[0] << endl;

   //find and display current rank of player
   for(int i = 0; i < total_records; i++)
   {
       if(player[i] == name && score[i] == final_score)
       {
           cout << "You ranked #" << i+1 << " of "
               << total_records << endl;
           break;
       }
   }

   //close fin_scores file
   fin_scores.close();

}

summary.txt
second 10000000
THIRD 0
fourth 5000
1
1
a 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote