Quiz score calculator! Need help!

Hello guys. I have an assignment due soon. I have to read a file and convert it to an array, then I need to compare the answers in that array to the correct answers array. After that I need to calculate the score by comparing the answers. Also the score must be a percentage from 0 to 100. If an answer is incorrect I need to display "incorrect" next to that answer. Also, my professor mentioned something about not using global variables. I don't really know what they are.
Any help will be much appreciate it.
I will keep working on my code.
Thank you!



#include <iostream>
#include <fstream> // to read input files and create output files
#include <string>
#include <iomanip> //to set precision and width
using namespace std;

void quizScore(double);



int main()
{
const int ARRAY_SIZE = 13;
string answers[ARRAY_SIZE];
string quizanswers[ARRAY_SIZE] = { "C++", "for", "if", "variable", "function", "return", "array", "void", "reference", "main", "prototype" };
const int correct = 0;
const int incorrect = 0;
int count = 0;
double score = 0;
ifstream inputFile;
ofstream outputFile;


inputFile.open("answers.dat");//to open the file "answers.dat"
while (count < ARRAY_SIZE && inputFile >> answers[count])
count++;
inputFile.close();

outputFile.open("Report.dat"); //to print the file "report"
outputFile << "!!! Ana Amaya's Most Excellent Quiz Reporter !!!\n ";
outputFile << "Report for" << " " << answers[0] << " " << answers[1] << endl;
outputFile << "CORRECT ANSWERS" << setw(16) << "STUDENT ANSWERS" << endl;

for (count = 0; count < ARRAY_SIZE; count++)
outputFile << quizanswers[count] << endl;

for (count = 2; count < ARRAY_SIZE; count++)
outputFile << answers[count] << endl;
















outputFile.close();
system("pause");

return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream> // to read input files and create output files
#include <string>
#include <iomanip> //to set precision and width
using namespace 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;
    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()
{
    const int ARRAY_SIZE = 13;  //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("CorrectAnswers.txt", correct_answers, ARRAY_SIZE);

    //after that read given answers
    read_answers("Answers.txt", answers, ARRAY_SIZE);

    double correct = find_correct_answers(correct_answers, answers, ARRAY_SIZE);
    score = 100 * correct / ARRAY_SIZE ;

    cout << "\nScore = " << score << "%"<< endl;

    return 0;
}
I'm not near a computer right now, but did your code worked? I don't see the functions below main. Also, I assume I read the assignment wrong, but we don't have to write the results to another file. It will be a regular output on the screen. I figured how to read the file, but I still need to compare each string in the array and that's what I keep doing wrong. Also, both functions(read file and compare answers, wont retuen anything). My teacherwants another function to print the report. It's confusing.
Topic archived. No new replies allowed.