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.


#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

ifstream ifs(filename);
if (!ifs)
{
cout << "Couldn't open answer file " + filename << endl;
return;
}

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;
}

Please post the data files you're using.

A couple of obvious problems:
1) You're using the same function to read both files. answers.txt contains the student name, but correct answers.txt does not.

2) The instructions say the student may or may not have a last name. Your read routine does not allow for student names at all.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.