Comparing Arrays

I am working on a project for school in which you must take an answer key, students names and their answers from a text file and output how many the students got right. I have the grade key and all the students answers in different arrays, i am having trouble making a loop to compare the arrays and add 1 for every correct answer. Also it is suppose to be ordered in a void function but i'm not even worried about that until I figure out this loop. Any help would be great, thanks.

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

//function prototypes
void order(int,int,int);

int main()
{
string n1,n2,n3,n4; //students names
char Akey[7]; //Anwser key
char Quest[1], n1A[7], n2A[7], n3A[7], n4A[7];//Student Answers
int s1 = 0,s2 = 0,s3 = 0,s4 = 0;

//declare global variables
ifstream inFile;
inFile.open("prog10Data.txt");
//----FILE NOT FOUND---
if (!inFile){
   cout << "Could not open " << inFile << "!" << endl;
   return 0;}
//---------END---------
inFile >> Quest >> Akey >> n1 >> n1A >> n2 
>> n2A >> n3 >> n3A >> n4 >> n4A;

if (Akey[1] == n1A[1])
{
   s1++;
}
if (Akey[2] == n1A[2])
{
   s1++;
}
if (Akey[3] == n1A[3])
{
   s1++;
}
if (Akey[4] == n1A[4])
{
   s1++;
}
if (Akey[5] == n1A[5])
{
   s1++;
}
if (Akey[6] == n1A[6])
{
   s1++;
}
if (Akey[7] == n1A[7])
{
   s1++;
}




//OUTPUT

cout << n1 << setw(8) << s1 << endl;
cout << n2 << setw(7) << s2 << endl;
cout << n4 << setw(6) << s4 << endl;
cout << n3 << setw(5) << s3 << endl;

inFile.close();
system("pause");
return 0;
}
Last edited on
1
2
3
4
5
6
for//a for loop because you are preforming an action for each element in a set ( form 0 to 6 )
(int i = 0;//start at 0
i < 7;//7 elements total. if this condition is false, the loop ends.
i++){//in every loop go one step forward..
   if(Akey[i] == n1A[i]) s1++;//what you did
}
Topic archived. No new replies allowed.