Please can anyone put me through this program. One of your professors at the Uni
ID: 3696622 • Letter: P
Question
Please can anyone put me through this program. One of your professors at the University you attend has asked you to write a program to grade student answers to a series of questions. There are three (3) tests that the student must select from and the answers to each of the tests are loaded from a text file into a separate array. That is, each test could be a separate array (or you may combine into a 2-dimensional array) and the results are unique. The three (3) selections will be extremely similar. You just need to setup a menu system to allow the student to select one of the three tests from your menu.
Your task is to build a menu system to select one and only one of the tests based upon the input from the student. [You need to process the student-requested test but still setup the logic/code for three (3) tests.] Based upon the student-value entered from the menu, you then ask the student five (5) questions and validate the responses to the questions based upon the results compared to the answers.
The input to the obtained from an external file.That is, say for question three (3) of test 2 the answer is “C” then you need to validate that answer “C” is correct. If other answers are entered then the student’s answer is incorrect.You’ll create the Answer Keys!
Question #4 allows for EITHER one of two (2) answers being correct. Note: Since a correct answer for Question #4 is coming into your program via the input file, you’ll need to hard-code the test for the second allowable answer. That is, if Question 4’s correct answer is “D”, then you’ll need to hard-code an allowable second answer of, say, “B”. [It is possible to input the answers totally from the input file, but I’m not asking you to do this!]
The FINAL question (question #5 for each test)has two (2) answers being correct so the student must enter two (2) responses and both responses must be correct in order to have a correct answer. Same situation for Question #5 as for Question #4 but BOTH answers must be correct. You’ll need to hard-code the second answer here also.
Here’s the general layout of the task:
Test selection: Each student has one (1) try and selects only one (1) test. You’ll need to make sure the student’s entry is valid and have a way to terminate the program if the student doesn’t want to take the test at this time.
Ask for name: Be sure to ask the student their name
Create Questions: You create the questions but the correct answer must match what was initially setup in an array. That is, if “C” is the answer, then your answer for “C” must be the correct answer. Generate the questions so that only ONE (1) response is allowed per question for Questions 1, 2 and 3. Require two (2) responses for Questions 4 and 5.
Questions 1-3 are multiple choice with only one response being correct
Question 4 can have EITHER one of two answers as correct.
If an incorrect answer is provided, then no credit
If one of the two answers is provided but not both, then provide full credit.
Otherwise, no credit
Question 5 must have two (2) answers to be correct and BOTH must be selected.
If BOTH responses are correct then the question is answered correctly.
If ONE response is correct then provide half credit.
If none of the responses are correct then provide no credit.
Questions: I’m not too interested in the questions. I’m interested in your logic and conditional setup for the questions. An example of questions could be adding or multiplying etc. to get some value.
Results tracking:
Display a message for EACH question if it is correct or not immediately after the student provides the answer.
You need to keep track of the number of questions that are correct and incorrect.
Produce the output at the end of the set of questions (by test) of the total number correct and incorrect. Format nnn.nn.Include the student’s name in the output sentence.
Produce a percentage of correct and missed. Format as nnn.nn%. Include the student’s name in the output.
Output: Output should be via the console AND via a file showing you tested and validated your logic.
EXTRA CREDIT (5 points): For Question #3, allow the user three (3) tries to get it correct. This Extra Credit may be placed in a special Graded Field in ACE.
Here’s a very brief example:
Question #1: “What is the product of three (3) times five (5)?: “
Responses to A, B, C, and E: Incorrect. Issue a message such as no credit, incorrect response. Save the incorrect response calculation into a variable and continue to the next question.
Response to D is correct.
Explanation / Answer
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
======================================================================*/
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
======================================================================*/
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
======================================================================*/
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
======================================================================*/
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
======================================================================*/
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
======================================================================*/
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
======================================================================*/
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();
}
answers.txt
C
A
C
A
B
d
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
summary.txt
second 10000000
THIRD 0
fourth 5000
1
1
a 1