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

I Need some code added to my program below so it shows the number of Correct Ans

ID: 3778114 • Letter: I

Question

I Need some code added to my program below so it shows the number of Correct Answers Below the Student ID and Above the Grade Thanks

INSTRUCTIONS GIVEN:

I need some code added to the program. so The output should be the student's ID, followed by the correct number of answers, follwed by the test score, followed by the grade. The test grading scale is as follows : 90 % - 100 % = A, 80 % - 89.99 % = B, 70 % - 79.99 % = C, 60 % - 69.99 % = D; and 0 % - 69.99 % = F. Directions, program ,output as of now and input.txt file are below

Directions for program

You are to write a program that can be used to grade a True/False test. The user is to be prompted for how many questions are on the exam. The input to the program is a file where the first record contains the answer key and the remaining records contain a student ID and the student responses. The first record contains answers in the form :

TFFTFFTTTTFFTFTFTFTT

The other records are formatted as :

17236 TFTFTF TTFTFFTTTTFTF

where the srudent ID is 17236 and the student's responses follow the space after the student ID. Note that on this example, the student did not answer question 7. Each correct answer is awarded 2 points, each wrong answer gets 1 point deducted and no answer gets zero points.

INPUT.txt file

TTFTFTTTFTFTFFTTFTTF
54102 T FTFTFTTTFTTFTTF TF
56278 TTFTFTTTFTFTFFTTFTTF
42366 TTFTFTTTFTFTFFTTF
42586 TTTTFTTT TFTFFFTF

Program

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

using namespace std;

int main()
{
char * answer = new char[20]; // Allocating an array of size 20 because there are 20 answers
int po = 0, i, t, tot = 0;
float per;

char * regno = new char[5]; // Allocating memory for registration number which is of size 5
char * Sanswer = new char[20]; //Students answers are 20 if not answered space
string line;

ifstream fr("input.txt"); //Opening an input stream for file input.txt

//Checking whether file could be opened or not.
//If file does not exist or don't have read permissions, displays an error message
if(fr.is_open())
{
//Reads one line from the file
while (getline(fr, line))
{
//First line contains answers only so po is checked with zero
if(po == 0)
{
//Reads 20 answers and stores it in answer array
for(t = 0; t < 20; t++)
answer[t] = line[t];

answer[t] = ''; //Placing character array terminating character
po++;

//Displays the correct answer sequence
cout<<" Correct Answers: " << answer << " ";
}
//From next line onwards
else
{
//Reads registration number and stores it in the regno array
for(i = 0; i <= 5; i++)
regno[i] = line[i];

regno[i] = ''; //Placing character array terminating character


//Reads Student answers and stores it in the Sanswer array
for(t = 0; t < 20; t++, i++)
{
//If there are insufficient number of answers, add space
if(i > strlen(line.c_str()+1))
Sanswer[t] = ' ';
else
Sanswer[t] = line[i];
}

Sanswer[t] = ''; //Placing character array terminating character

//Displays the answer given by the student
cout<<" Student Answered: " << Sanswer << " ";

//Initialize to zero for every student
tot = 0;

//Calculates the percent and grade
for(t = 0; t < 20; t++)
{
//Checks for the correct answer
if(answer[t] == Sanswer[t])
tot += 2; //Adds 2 mark for correct answer
//Checks for the Wrong answer
else if (answer[t] != Sanswer[t])
tot -= 1; //Subtracts 1 mark for the wrong answer
else //For not answered
tot += 0;
}

//Calculates percentage
per = tot/.40;

cout<<" Student Id: " << regno << " ";

//Checks for the grade
if(per >= 90 && per <= 100)
cout << " Grade: A ";
else if(per >= 80 && per <= 89.99)
cout << " Grade: B ";
else if(per >= 70 && per <= 79.99)
cout << " Grade: C ";
else if(per >= 60 && per <= 69.99)
cout << " Grade: D ";
else
cout << " Grade: F ";
}
}
}
}

Output

CATCAstudentScoresGradesibinADebugistudentscoresGrades.exe Correct Answers: TTFTFTTTFTFTFFTTF TTF Student Answered: T FTFTFTTTFTTFTTF TF Student Id: 54102 Grade: D Student Answered: TTFTFTTTFTFTFFTTFTTF Student Id: 56278 Grade: A Student Answered: TTFTFTTTFTFTFFTTF Student Id: 42366 Grade: C Student Answered: TTTTFTTT TFTFFFTF Student Id: 42586 Grade: F Process returned 0 (0x0) execution time 0.091 s Press any key to continue.

Explanation / Answer

#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;

int main()
{
   char * answer = new char[20]; // Allocating an array of size 20 because there are 20 answers
   int po = 0, i, t, tot = 0;
   float per;

   char * regno = new char[5]; // Allocating memory for registration number which is of size 5
   char * Sanswer = new char[20]; //Students answers are 20 if not answered space
   string line;

   ifstream fr("1.txt"); //Opening an input stream for file input.txt

   //Checking whether file could be opened or not.
   //If file does not exist or don't have read permissions, displays an error message
   if(fr.is_open())
   {
   //Reads one line from the file
   while (getline(fr, line))
   {
   //First line contains answers only so po is checked with zero
   if(po == 0)
   {
   //Reads 20 answers and stores it in answer array
   for(t = 0; t < 20; t++)
   answer[t] = line[t];

   answer[t] = ''; //Placing character array terminating character
   po++;

   //Displays the correct answer sequence
   cout<<" Correct Answers: " << answer << " ";
   }
   //From next line onwards
   else
   {
   //Reads registration number and stores it in the regno array
   for(i = 0; i <= 5; i++)
   regno[i] = line[i];

   regno[i] = ''; //Placing character array terminating character


   //Reads Student answers and stores it in the Sanswer array
   for(t = 0; t < 20; t++, i++)
   {
   //If there are insufficient number of answers, add space
   if(i > strlen(line.c_str()+1))
   Sanswer[t] = ' ';
   else
   Sanswer[t] = line[i];
   }

   Sanswer[t] = ''; //Placing character array terminating character

   //Displays the answer given by the student
   cout<<" Student Answered: " << Sanswer << " ";

   //Initialize to zero for every student
   tot = 0;
   int totalCorrectAnswer = 0;
   //Calculates the percent and grade
   for(t = 0; t < 20; t++)
   {
       //Checks for the correct answer
       if(answer[t] == Sanswer[t])
       {
           tot += 2; //Adds 2 mark for correct answer
           totalCorrectAnswer++;
       }
       //Checks for the Wrong answer
       else if (answer[t] != Sanswer[t])
       tot -= 1; //Subtracts 1 mark for the wrong answer
       else //For not answered
       tot += 0;
   }

   //Calculates percentage
   per = tot/.40;

   cout<<" Student Id: " << regno << " ";
   cout << " Total Correct answers: " << totalCorrectAnswer << endl;
   //Checks for the grade
   if(per >= 90 && per <= 100)
   cout << " Grade: A ";
   else if(per >= 80 && per <= 89.99)
   cout << " Grade: B ";
   else if(per >= 70 && per <= 79.99)
   cout << " Grade: C ";
   else if(per >= 60 && per <= 69.99)
   cout << " Grade: D ";
   else
   cout << " Grade: F ";
   }
   }
   }
}

/*
output:

Correct Answers: TTFTFTTTFTFTFFTTFTTF

Student Answered: T FTFTFTTTFTTFTTF TF

Student Id: 54102
Total Correct answers: 15

Grade: D


Student Answered: TTFTFTTTFTFTFFTTFTTF

Student Id: 56278
Total Correct answers: 20

Grade: A


Student Answered: TTFTFTTTFTFTFFTTF  

Student Id: 42366
Total Correct answers: 17

Grade: C


Student Answered: TTTTFTTT TFTFFFTF  

Student Id: 42586
Total Correct answers: 14

Grade: F

*/