Can someone please tell me what i am doing wrong. It will run but when it does they all say incorrect and 0%.

closed account (30D23TCk)
I have to write a program helps your instructor grade quizzes. The program will read a file containing a single student’s answers to a recently given quiz. It will compute the student’s grade on the test as a percentage, and then print a report Your program should read the file answers.dat. This file contains the name of the student who took the quiz, and his answers for each question. The answers file has the following format: The student name is always the first line. A student name may or may not have last names. A quiz always has 11 questions. There is one answer per line. Each answer is one word and all lowercase.


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
#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);
void read_answers(const string& filename, string* arr, int size)
{
//arr - pointer to string array
//size = size of that array

for (int i = 0; i < size; ++i)
{
ifs >> arr[i];
}
}

//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 = 11;  
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;

system("pause");
return 0;
}

Last edited on
@detroit31312

Nowhere do I see you actually opening the file. You're just naming it. Also, you don't have to declare your function, as in line 7, unless you place the function BELOW main().
Topic archived. No new replies allowed.