Add error checking to this program

I have the following two programs in which I want someone with the knowledge to help me check and add some lines


CODE #1
code to see did it catch the first error?
code to see did it catch another error?

CODE #2
Is my sizing of MaxNum correct?
Is the Vector declaration correct?
Is the push_back syntax correct?
In which line was the first error detected?
In which line was the second error detected?
In which line was the third error detected?
Was the percentage correct accurate?

CODE #1
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



#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
  // create char arrays
  char correct[20];
  char student[20];

  // read files
  ifstream correctFile, studentFile;
  correctFile.open("CorrectAnswers.txt");
  studentFile.open("StudentAnswers.txt");
  for (int i = 0; i < 20; i++)
  {
    correctFile >> correct[i];
    studentFile >> student[i];
  }
  correctFile.close();
  studentFile.close();  

  // display your name
  string name = "Name";
  cout << endl << name << endl;

  // display header
  cout.width(10); cout << left << "Question";
  cout.width(10); cout << left << "Correct";
  cout.width(10); cout << left << "Incorrect" << endl;

  // display list of missed questions
  int missed = 0;
  for (int i = 0; i < 20; i++)
  {
    if (student[i] != correct[i])
    {
      cout.width(10); cout << left << (i+1);
      cout.width(10); cout << left << correct[i];
      cout.width(10); cout << left << student[i] << endl;
      missed++;
    }
  }
  cout << endl << "Missed Questions: " << missed << endl;
  
  // calculate percentage
  double percentage = ((20-(double)missed)/20)*100;
  cout << "Correct Answers : " << percentage << "% " << endl;

}





CODE #2
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
94
95


#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() 
{
  // create vectors
  vector<char> correct;
  vector<char> student;

  // read files
  ifstream correctFile, studentFile;
  correctFile.open("CorrectAnswers.txt");
  studentFile.open("StudentAnswers.txt");
  for (int i = 0; i < 20; i++)
  {
    char c, s;
    correctFile >> c;
    studentFile >> s;

    // insert to vector
    correct.push_back(c);
    student.push_back(s);
  }
  correctFile.close();
  studentFile.close();  

  // display your name
  string name = "Name";
  cout << endl << name << endl;

  // display header
  cout.width(10); cout << left << "Question";
  cout.width(10); cout << left << "Correct";
  cout.width(10); cout << left << "Incorrect" << endl;

  // display list of missed questions
  int missed = 0;
  int count = correct.size();
  for (int i = 0; i < count; i++)
  {
    if (student[i] != correct[i])
    {
      cout.width(10); cout << left << (i+1);
      cout.width(10); cout << left << correct[i];
      cout.width(10); cout << left << student[i] << endl;
      missed++;
    }
  }
  cout << endl << "Missed Questions: " << missed << endl;
  
  // calculate percentage
  double percentage = ((count-(double)missed)/count)*100;
  cout << "Correct Answers : " << percentage << "% " << endl;

}



The following files are already provided by you:


    CorrectAnswers.txt
    StudentAnswers.txt



Explanation:


Here is a sample output of both the programs above:


StudentIncorrectAnswers.png


The program performs the following tasks:

    reads the correct answers and student answers
    counts the missed questions
    displays the missed questions
    displays the correct percentage


Let me know if you have additional questions, requests, or issues encountered so that I can address your concerns right away. 

I can give you a link to the updated code based on your concerns if you need it. 


Thanks!
StudentIncorrectAnswers.png


CorrectAnswers.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
D
B
C
D
D
C
B
A
C
B
A
D
A
C
D
A
A
D
C
B



StudentAnswers.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
A
B
C
D
D
C
B
A
C
B
A
D
B
C
D
A
A
D
C
B

Last edited on
there is no 'maxnum' that you asked about.
your vectors look fine but:
vector<char> correct(20); //as if array[20] -- if its a known, never changing size, this is ok to do

yes, push back looks good.
also, you don't need 2 char variables there. You can reuse 1 char variable to read, push back, read, push back, like that. not a big deal, just a note on being careful about excessive variables (clutter, if nothing else).

line 45 detects errors.
percentage looks correct, but you should use c++ casts in formal code (type) is a C cast, and I use them too, but its not c++ anymore. I would have cast the denominator, but as long as any of the terms are doubles it should resolve.

that isnt everything you asked but --- test it? Actually run your code where you know the answer it should give (% correct, etc) and see if it is working on a few example files. Even small code like this, spotting all the errors by hand is a chore, but all in all, it looks good, its probably either working or very nearly, and you did a good job as far as I can see.
Thanks jonin, just wanted your thoughts!
You don't need to use an array/vector. You can read a line from each and compare. Consider:

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
include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream correctFile("CorrectAnswers.txt");
	ifstream studentFile("StudentAnswers.txt");

	if (!correctFile.is_open() || !studentFile.is_open()) {
		cout << "Cannot open files\n";
		return 1;
	}

	// display your name
	cout << endl << "Name" << endl;

	// display header
	cout.width(10); cout << left << "Question";
	cout.width(10); cout << left << "Correct";
	cout.width(10); cout << left << "Incorrect" << endl;

	int missed {};
	int numquest {};

	// read files and process
	for (char corr {}, stud {}; (correctFile >> corr) && (studentFile >> stud); ++numquest)
		if (stud != corr) {
			cout.width(10); cout << left << numquest + 1;
			cout.width(10); cout << left << corr;
			cout.width(10); cout << left << stud << endl;
			++missed;
		}

	cout << "\nTotal Questions: " << numquest;
	cout << "\nMissed Questions: " << missed << '\n';

	// calculate percentage
	const double percentage = ((numquest + 0.0 - missed) / numquest) * 100;
	cout << "Correct Answers : " << percentage << "% " << endl;
}



Name
Question  Correct   Incorrect
1         D         A
13        A         B

Total Questions: 20
Missed Questions: 2
Correct Answers : 90%


Last edited on
Topic archived. No new replies allowed.