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
57
58
59
60
61
62
63
#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];   //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 = 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("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
Reading a whole line (needed for student names) from an ifstream and 11 answers:
1
2
3
std::string str;
ifst.get_line( str);  // reads a whole line into a string
for (int i = 0; i <11; ++i) { ifs >> given_answers[i]; }


Also I would use std::vector instead of raw arrays:
1
2
std::vector<std::string> given_answers(11);  // initialises an 'array' of size 11 empty strings 
for(auto str : given_answers) std::cout << str << '\n';  // Example use of such vector  


Read in the answers.dat:
1
2
3
4
5
6
7
std::vector<std::string> correct_answers;  // An empty vector
std::ifstream ifstr_corr_answ("answers.dat");  // Opens the ifstream
if (!ifstr_corr_answ) return;
while (ifstr_corr_answ) {       // reads until the stream is empty
    ifstr_corr_answ >> std::string str;
    correct_answers.push_back( str );    // adds the new string at end of its array
}


How to operate on such vector:
1
2
3
4
5
6
7
8
9
// passing the args as references (!)
int get_correct_answers(const std::vector<std::string>& answers, const std::vector<std::string>& solutions)
{
    if solutions.size() < answers.size() return -1;  // vectors shouldn't have different sizes
    int count = 0;
    for ( int i = 0; i < solutions.size(); ++i)  {
        if (solutions[i] == answers[i]) ++count;
    }
    return count;

Look at cppreference.com for right use of std::vector. That's an often used type (beside string).

Hope that helps you :-)
Last edited on
Topic archived. No new replies allowed.