Hello, I am brand new to programing and I been having a hard time with c++, so i would love some help, since I really want to learn how to program in this language.
For this program I need to create a quiz grader, I have to compare the answers given in a file vs the correct answers given in an other file.I found a program that does exactly that but I also have some questions about it.
#include <iostream>
#include <fstream> // to read input files and create output files
#include <string>
#include <iomanip> //to set precision and width
usingnamespace std;
void read_answers(const string& filename, string* arr, int size)
{
//arr - pointer to string array
//size = size of that array
ifstream ifs(filename);
if (!ifs)
{
cout << "Couldn't open answer file " + filename << endl;
return;
}
for (int i = 0; i < size; ++i)
{
ifs >> arr[i]; //reading string from file inside array
}
}
//count correct answers and display wrong ones
double find_correct_answers(string* correct_answers, string* given_answers, int size)
{
double correct = 0;
cout << "Welcome to Alicia's grading quizz " << endl;
for (int i = 0; i < size; ++i)
{
if (correct_answers[i] == given_answers[i])
++correct;
else
cout << "Question " << i + 1 << " incorrect!" << endl;
}
return correct;
}
int main()
{
constint ARRAY_SIZE = 11; //how many test questions are there
double score{ 0 };
string correct_answers[ARRAY_SIZE];
string answers[ARRAY_SIZE];
//first read correct answers stored in file CorrectAnswers.txt
read_answers("test.txt", correct_answers, ARRAY_SIZE);
//after that read given answers
read_answers("given.txt", answers, ARRAY_SIZE);
double correct = find_correct_answers(correct_answers, answers, ARRAY_SIZE);
score = 100 * correct / ARRAY_SIZE;
cout << "\nScore = " << score << "%" << endl;
system("pause");
return 0;
My first question is how can I display all the given answers and compare them to the correct answers something like this:
CORRECT ANS STUDENT ANS
C++ C++
for for
if if
variable variable
function function
return cheese INCORRECT
array array
void robots INCORRECT
reference reference
main main
prototype prototype
this is my first semester dealing with programing and C++ is giving me a headache.Thanks for the help.