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

#include <iostream> #include <string> #include <fstream> #include <iomanip> usin

ID: 3828309 • Letter: #

Question

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
/*
This program qives a student a true/false quiz. Student enters T for true answer and F for the
false answer. Program stores all three: questions, answers, and student responses in three different arrays.
The questions are stored in a string array and read from a file. [One question per line].
The answers are stored in an int array and read from a file.
In answers file 1 represent true and 0 false.
Both array have same length.
Student responses are recorded in another (third) int array.
The answer and student response arrays are compared. Student score is computed.
If student scores 60% or more the message is given that student passed. Otherwise
message is given that student failed.
*/
const int MAX = 20;
void openFile(ifstream & input, const string & message);
int fillQuestionsArray(string questions[], ifstream & inq);
int fillAnswersArray(char answers[], ifstream & inq);
void fillResponseArray(const string questions[], int response[], int lenQ);
int GradeResponses(const char answers[], const int response[], int len);
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len);
int main(){
cout << "Welcome to the quiz." << endl;
ifstream in;
openFile(in, "TestBank.txt");
string questions[MAX] = {};
int lenQ = fillQuestionsArray(questions, in);
{
for (int i = 0; i < MAX; i++);
}
in.close();
openFile(in, "Answers.txt");
char answers[MAX];
int lenA = fillAnswersArray(answers, in);
if (lenQ != lenA){
cout << "Number of questions and answers are different. Exiting the program." << endl;
exit(0);
}
do{
   cout<<"Enter the number of questions 10 = 1 / 20 = 2";cin>>lenQ;
}while(!(lenQ>=1 && lenQ<=2));
lenQ*=10;
int response[MAX] = {};
fillResponseArray(questions, response, lenQ);
double score = 0;
score = GradeResponses(answers, response, lenQ);
printAnswersAndResponses(questions, answers, response, lenQ);
cout << "You scored " << score << " points, out of maximum " << lenQ << " points." << endl;
double percent = (double(score) / lenQ) * 100;
if (percent >= 60.0){
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "You passed the quiz." << endl;
}
else{
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "Sorry you failed the quiz." << endl;
}
in.close();
cout << "--------------------------------------------------------------------------------------------------------" << endl;
return 0;
}
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len){
cout << "*****************************************************" << endl;
cout << "Here are correct answers and your responses." << endl;
cout << "*****************************************************" << endl;
cout << setw(55) << left << "Question" << setfill(' ') << setw(18) << left << "Correct Answer" << setfill(' ') << left << setw(15) << "Your Response" << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i<len; i++){
cout << setw(55) << left << questions[i] << setfill(' ') << setw(18) << left << answers[i] << setfill(' ') << setw(15) << left << response[i] << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
double GradeResponses(const char answers[], const int response[], int len){
double score = 0;
for (int i = 0; i<len; i++){
if (answers[i] == response[i]){
if (len>10)
           score+=0.5;
       else
       score++;
}
}
return score;
}
void fillResponseArray(const string questions[], int response[], int lenQ){
cout << "Please answer the questions as asked. Questions are true or false. "
<< "Enter T for a true answer and F for a false answer." << endl;
int ques[lenQ];
for (int i = 0; i<lenQ; ++i){
   do{
   int k=0;bool oldQues;
       srand();
       do{ //get non-repeatable random number
       oldQues=false;
               k=rand()%lenQ;
               for (int t=0;t<i;t++){
                   oldQues=(ques[t]==k);
                   break;
               }
               ques[i]=k;
       } while (oldQues);
       char option;
       cout << questions[k] << " : ";
       cin >> option;
       switch(option){
           case 'T':
           case 't': response[k]=1;break;
           case 'F':
           case 'f': response[k]=0;break;
           default : cout<<"Invalid input";option='X';
       }
   } while (option=='X');
}
}
int fillAnswersArray(char answers[], ifstream & inA){
char ch = ' ';
int i = 0;
while ((ch = inA.peek()) != EOF && i<MAX){
inA >> answers[i];
i++;
}
return i;
}
int fillQuestionsArray(string questions[], ifstream & inq){
char ch = ' ';
int i = 0;
while ((ch = inq.peek()) != EOF && i<MAX){
getline(inq, questions[i]);
i++;
}
return i;
}
void openFile(ifstream & input, const string & message){
bool done = false;
string In_File = "";
//Loop to get the name of a file that truly exists
// and check that file is not empty.
while (!done){
input.clear();
cout << "Please input the name of the data file with " << message << " to be read : ";
getline(cin, In_File);
cout << "The file name entered is : " << In_File << endl;
input.open(In_File.c_str());
if (input.fail()){
cout << "The file " << In_File << " does not exist. ";
done = false;
}
else{
input.peek();//peek at the input file
if (input.eof()){//If file is empty then the function eof() returns true
cout << "The file has no data in it ";
done = false;
input.close();
}
else{
done = true;
}
}
}//end of while loop
cout << "File " << In_File << " opened and has data in it." << endl;
}

I have a few erroes, any help?
Error (active)   E0311   cannot overload functions distinguished by return type alone   ConsoleApplication3   c:UsersDaveDocumentsVisual Studio 2017ProjectsConsoleApplication3ConsoleApplication3ConsoleApplication3.cpp 23

Severity   Code   Description   Project   File   Line   Suppression State
Error (active)   E0028   expression must have a constant value   ConsoleApplication3   c:UsersDaveDocumentsVisual Studio 2017ProjectsConsoleApplication3ConsoleApplication3ConsoleApplication3.cpp   92  

Severity   Code   Description   Project   File   Line   Suppression State
Error (active)   E0165   too few arguments in function call   ConsoleApplication3   c:UsersDaveDocumentsVisual Studio 2017ProjectsConsoleApplication3ConsoleApplication3ConsoleApplication3.cpp   96  

Severity   Code   Description   Project   File   Line   Suppression State
Error (active)   E0020   identifier "option" is undefined   ConsoleApplication3   c:UsersDaveDocumentsVisual Studio 2017ProjectsConsoleApplication3ConsoleApplication3ConsoleApplication3.cpp   116  

Severity   Code   Description   Project   File   Line   Suppression State
Error   C1010   unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?   ConsoleApplication3   c:usersdavedocuments isual studio 2017projectsconsoleapplication3consoleapplication3consoleapplication3.cpp   166  

Explanation / Answer

Hi

I have done following modifications in the code.

1) Line - 96

srand() function's prototype is void srand(unsigned int seed). So it requires a seed to generate random numbers. I have provided '1' as seed to this function.

2) Line - 118

I have changed the condition inside the while as the variable 'option' is declared inside the while loop,so it cannot be accessed outside the while loop. I have changed the condition to 'while(true)' and have put a break inside the while loop if the condition 'if(option=='X')' satisfies. I hope this is what you wanted.

3) Line 77

You have declared the function 'int GradeResponses(const char answers[], const int response[], int len);; with int as return type but while you using,you have changed it to 'double' in Line 77. I have changed the return type to int again. You can use it or change the return type in the function prototype to double if you really want it.

4) For "'#include "stdafx.h"' to your source? " error include this header file in the source code.

Final code is below. Please tell me if you need any further help.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

#include<stdafx>

using namespace std;
/*
This program qives a student a true/false quiz. Student enters T for true answer and F for the
false answer. Program stores all three: questions, answers, and student responses in three different arrays.
The questions are stored in a string array and read from a file. [One question per line].
The answers are stored in an int array and read from a file.
In answers file 1 represent true and 0 false.
Both array have same length.
Student responses are recorded in another (third) int array.
The answer and student response arrays are compared. Student score is computed.
If student scores 60% or more the message is given that student passed. Otherwise
message is given that student failed.
*/
const int MAX = 20;
void openFile(ifstream & input, const string & message);
int fillQuestionsArray(string questions[], ifstream & inq);
int fillAnswersArray(char answers[], ifstream & inq);
void fillResponseArray(const string questions[], int response[], int lenQ);
int GradeResponses(const char answers[], const int response[], int len);
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len);
int main(){
cout << "Welcome to the quiz." << endl;
ifstream in;
openFile(in, "TestBank.txt");
string questions[MAX] = {};
int lenQ = fillQuestionsArray(questions, in);
{
for (int i = 0; i < MAX; i++);
}
in.close();
openFile(in, "Answers.txt");
char answers[MAX];
int lenA = fillAnswersArray(answers, in);
if (lenQ != lenA){
cout << "Number of questions and answers are different. Exiting the program." << endl;
exit(0);
}
do{
cout<<"Enter the number of questions 10 = 1 / 20 = 2";cin>>lenQ;
}while(!(lenQ>=1 && lenQ<=2));
lenQ*=10;
int response[MAX] = {};
fillResponseArray(questions, response, lenQ);
double score = 0;
score = GradeResponses(answers, response, lenQ);
printAnswersAndResponses(questions, answers, response, lenQ);
cout << "You scored " << score << " points, out of maximum " << lenQ << " points." << endl;
double percent = (double(score) / lenQ) * 100;
if (percent >= 60.0){
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "You passed the quiz." << endl;
}
else{
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "Sorry you failed the quiz." << endl;
}
in.close();
cout << "--------------------------------------------------------------------------------------------------------" << endl;
return 0;
}
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len){
cout << "*****************************************************" << endl;
cout << "Here are correct answers and your responses." << endl;
cout << "*****************************************************" << endl;
cout << setw(55) << left << "Question" << setfill(' ') << setw(18) << left << "Correct Answer" << setfill(' ') << left << setw(15) << "Your Response" << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i<len; i++){
cout << setw(55) << left << questions[i] << setfill(' ') << setw(18) << left << answers[i] << setfill(' ') << setw(15) << left << response[i] << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
int GradeResponses(const char answers[], const int response[], int len){ //Changed the return type from double to int
double score = 0;
for (int i = 0; i<len; i++){
if (answers[i] == response[i]){
if (len>10)
score+=0.5;
else
score++;
}
}
return score;
}
void fillResponseArray(const string questions[], int response[], int lenQ){
cout << "Please answer the questions as asked. Questions are true or false. "
<< "Enter T for a true answer and F for a false answer." << endl;
int ques[lenQ];
for (int i = 0; i<lenQ; ++i){
do{
int k=0;bool oldQues;
srand(1); // Provide some argument to srand() function
do{ //get non-repeatable random number
oldQues=false;
k=rand()%lenQ;
for (int t=0;t<i;t++){
oldQues=(ques[t]==k);
break;
}
ques[i]=k;
} while (oldQues);
char option;
cout << questions[k] << " : ";
cin >> option;
   if(option=='X')
   break;
switch(option){
case 'T':
case 't': response[k]=1;break;
case 'F':
case 'f': response[k]=0;break;
default : cout<<"Invalid input";option='X';
}
} while (true); // Changed this while condition as Option variable is declared inside the loop so it cannot be accessed outside the loop
}
}
int fillAnswersArray(char answers[], ifstream & inA){
char ch = ' ';
int i = 0;
while ((ch = inA.peek()) != EOF && i<MAX){
inA >> answers[i];
i++;
}
return i;
}
int fillQuestionsArray(string questions[], ifstream & inq){
char ch = ' ';
int i = 0;
while ((ch = inq.peek()) != EOF && i<MAX){
getline(inq, questions[i]);
i++;
}
return i;
}
void openFile(ifstream & input, const string & message){
bool done = false;
string In_File = "";
//Loop to get the name of a file that truly exists
// and check that file is not empty.
while (!done){
input.clear();
cout << "Please input the name of the data file with " << message << " to be read : ";
getline(cin, In_File);
cout << "The file name entered is : " << In_File << endl;
input.open(In_File.c_str());
if (input.fail()){
cout << "The file " << In_File << " does not exist. ";
done = false;
}
else{
input.peek();//peek at the input file
if (input.eof()){//If file is empty then the function eof() returns true
cout << "The file has no data in it ";
done = false;
input.close();
}
else{
done = true;
}
}
}//end of while loop
cout << "File " << In_File << " opened and has data in it." << endl;
}