Reading from file to an array and computing the grades

Given a multiple-choice test of 10 questions with 5 possible choices (A-E). The answers are given in a text file a long with the student names. Write a C++ program that does the following:
1.Reads the key answers to an array from the input file. The key is the first line in the file
2.For each student read his/her answers and compare their answers with the key array
3.Stores each the answers of each student in an array of (chars)
4.Sends this array to a function to compute number of correct answers per student
5.Computes % of correct answers for each student
6.Stores the scores in an array of (ints)
7.Displays the list of the students along with their scores (counts and percentages)

My code is not reading the names and grades correctly. I am also unsure of how to calculate the grades of the students. Here is the beginning of my code below.

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
64
65
66
67
68
69
# include <cmath>
# include <iostream>
# include <iomanip>
# include <cstring>
# include <cctype>
# include <fstream>
# include <string>
using namespace std;
int main()
{
string x;
string temp[1] = {x};
char y = 0;
char key[10] = {y};

string a;
string name[1] = {a};
char b = 0;
char grade[10] = {b};

ifstream fin;
fin.open("fin.txt");
 
for( int i = 0; i < 10; ++i)
{
   fin >> x;
   fin >> y;
 
}
 
while (fin)
{
    for( int j = 0; j < 10; ++ j)
    {
      fin >> a;
      fin >> b;
       
      if (grade[0] = key[0] )
          ;
      if (grade[1] = key[1] )
          ;
      if (grade[2] = key[2] )
          ;
      if (grade[3] = key[3] )
          ;
      if (grade[4] = key[4] )
          ;
      if (grade[5] = key[5] )
          ;
      if (grade[6] = key[6] )
          ;
      if (grade[7] = key[7] )
          ;
      if (grade[8] = key[8] )
          ;
      if (grade[9] = key[9] )
          ;
   
      // Need some type of function to cout their grade
    }
   

   
   
}

system ("pause");
return 0;
}
Man I'm bored.


Input:

1
2
3
4
5
ABCDEABCDE
Dave 
ABCDCDABAD
Jim 
CCADACADAB


Output:

1
2
3
4

Dave 40%
Jim 10%




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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<iostream>
#include<fstream>
#include <map>
#include <vector>
#include <cstdlib>

using namespace std;


const int Q_MAX = 10;

class GradeBook {
public:
        bool Read(string filename);
	void Write(ostream& out);
	void Calculate();

private:
	vector <char> key;
	map <string, vector<char> > sKeys;
	map <string, float> sGrade;
};

bool GradeBook::Read(string filename) {
	ifstream fin;	
	char buff;
	string name;
	vector<char> tmp;

	fin.open(filename.c_str());

	if (!fin) {
		cout << "error! file missing or corrupt";
		return false;
	}

	for (int i = 0; i < Q_MAX; i++) {
		fin >> buff;
		key.push_back(buff);
	}
	
	fin.ignore(256, '\n');
	fin >> name;

	while (fin) {	
		for (int i = 0; i < Q_MAX; i++) {
			fin >> buff;
			tmp.push_back(buff);
		}
		
		fin.ignore(256, '\n');
		sKeys[name] = tmp;
		tmp.clear();
		fin >> name;
	}
	
	return true;
}

void GradeBook::Write(ostream& out) {
	map <string, float>::iterator it;

	for (it = sGrade.begin(); it != sGrade.end(); it++) 
		out << it->first << ' ' << it->second << '%' << endl;
}

void GradeBook::Calculate() {
	map <string, vector<char> >::iterator it;
	float numCorrect = 0;
	
	for (it = sKeys.begin(); it != sKeys.end(); it++) {
		for (int i = 0; i < it->second.size(); i++) 
			if (key[i] == it->second[i])
				numCorrect++;
		
		sGrade[it->first] = ((numCorrect / Q_MAX) * 100); 
		numCorrect = 0;		
	}
}

int main() {

	GradeBook g;

        if (!g.Read("answers.txt"))
               return 1;

	g.Calculate();
	g.Write(cout);
	
	return 0;
}
Last edited on
This is great, but its too advanced for me lol. I want my work done, but I want to understand it as well. I haven't used the last three libraries that you have, so I my teacher would know it isn't my work.
Its actually pretty sloppy and overly-complicated, I just wanted to use maps since I just learned about them this semester.

You can make a struct that stores a students name and their answers as a std::string for convenience. you can treat a std::string like an array and just cycle through it comparing all the grades with the key.

If the input for the key is like:

ADCABBACABACABACAB


And a students answers is like:

ABBCCBCBCBCBCBCBB


read them in as a strings and:

1
2
3
for (int i = 0; i < str1.length(); i++)
       if (str1[i] == str2[i])
           correct++;


Put an extra if statement to make sure both strings are the same length though or you can end up with a seg fault. But this is a really easy way to get the job done. You could go with the smaller strings length but if they are of diff lengths something is wrong anyway.

You could validate that each students answer is the same length of the answer key right after you read them in as well.

Last edited on
You are reading in x then y for some reason. I'm not sure what those represent, but they are in a loop and so the first 18 lines are lost and then the next 2 are stored in x and y which are not used anywhere.

THis is what I would do:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int get_grade(string ans, string k)
{

  int i; j = 0;

  for (i = 0; i < k.length(), i++)
    if (ans[i] == k[i]) j++;

  return j*100/i;

}


int main()
{
  ifstream fin("fin.txt");
  string key, name, answer, name_set[100];
  int i, grade, grade_set[100];

  //First line is the key, get that
  fin >> key;

  for (i=0; !fin.eof(); i++)
  {
    //Second, read name and answer
    fin >> name;
    fin >> answer;

    //Send to function to calc grade
    grade = get_grade(answer, key);

    //Then store to an array:
    name_set[i] = name;
    grade_set[i] = grade;
  }

  //Display the list of names/
  for (int j = 0; j < i ; j++)
    cout << name_set[i] << "  " << grade_set[i] << endl;  



  return 0;
}


if that doesn't work for the inputs, use the cin.getline() function. You can find the references on this site.
Last edited on
Thanks for you help, but I couldn't get it to work lol. I just gave up.
Topic archived. No new replies allowed.