Problems Passing Arrays

I'm currently working on a program that reads in values to one array, compares those values to another array which has values read into it from a data file, and then places the boolean comparisons in a parallel array for later use.
Although the program compiles fine, and even runs(remarkably), it does not produce the intended results. The first array seems to read in values fine, but the second array which has its values read in from the file does not work properly, throwing everything else off afterwards.Here is the program's relevant code:(the size for the arrays is is set as const int qMAX = 20;
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
96
97
98
99
100
101
102
103
int main()
{
  ifstream file;
  char determine, answers[qMAX], key[qMAX] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',
   'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
  int countRt, findWrng, qWrng;
  bool check[qMAX];  
 
    readAnswers(answers, qMAX);
    fillCheck(key, answers, check, qMAX);
    countRt = countRight(check, qMAX);
    findWrng = findWrong(countRt, qMAX);
    determine = determinePass(check, qMAX);
    displayResults(determine, countRt, findWrng, qWrng);
  
  file.close();
  
  return (0);
} 

void readAnswers(char raAnswers[], int raSize)
{
  ifstream file;
  file.open("driving.dat");

  if (file.fail())
    {
      cout << "Open Failed." << endl;
      exit(0);
    }
  else 
      cout << "Open Successful." <<endl;
  for(int i=0; i<raSize; i++)
    file >> raAnswers[i];    
   
}

void fillCheck(char fcKey[], char fcAnswers[], bool fcCheck[], int fcSize)
{ 
  for(int w=0; w<fcSize; w++)
  {
    if(fcKey[w] == fcAnswers[w])
      fcCheck[w] = "T";
    else
      fcCheck[w] = "F";
  }

}

int countRight(bool crCheck[], int crSize)
{
  int countRt = 0;
  
  for(int r=0; r<crSize; r++)
      if(crCheck[r] == 'T')
        countRt++;

 return countRt;     
}

int findWrong(int fwCountRight, int fwSize)
{
  int findWrng;
  
  findWrng = fwCountRight - fwSize;
  
  return findWrng;
}

void questionsWrong(bool qwCheck[], int qwSize)
{
  
  for(int w=0; w<qwSize; w++)
    if(qwCheck[w] == 'F')
      cout <<(w + 1) <<" ";
}

char determinePass(bool dpCheck[], int dpSize)
{
  int pCount = 0;
  char determine;
  
  for(int p=0; p<dpSize; p++)
    if(dpCheck[p] == 'T')
    pCount++;
    
  if(pCount >= 15)
    determine = 'P';
  else 
    determine = 'F';
    
  return determine;
}

void displayResults(char drDeterminePass, int drCountRight, int drFindWrong, int drQuestionsWrong)
{
  bool check[qMAX];
  
  cout << "Pass(P)/Fail(F)" <<"    " <<"NumberCorrect" <<"    " <<"NumberWrong" <<"    " <<"QuestionsWrong" <<endl;
  cout << "------------------------------------------------------------------------" <<endl;
  cout << drDeterminePass <<setw(20) <<drCountRight <<setw(20) <<drFindWrong <<setw(20);
  questionsWrong(check, qMAX);
  cout <<endl;
Hello.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
  //ifstream file; // not used remove
  char determine; // declare one per line, easier to read
  char answers[qMAX] = {0};
  char key[qMAX] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',
   'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
  int countRt = 0;
  int findWrng = 0;
  int qWrng = 0;
  bool check[qMAX] = {0};  
 
    readAnswers(answers, qMAX);
    fillCheck(key, answers, check, qMAX);
    countRt = countRight(check, qMAX);
    findWrng = findWrong(countRt, qMAX);
    determine = determinePass(check, qMAX);
    displayResults(determine, countRt, findWrng, qWrng);
  
  //file.close();  // remove
  
  return (0);
} 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void readAnswers(char raAnswers[], int raSize)
{
  ifstream file;
  file.open("driving.dat");

  if (file.fail())
    {
      cout << "Open Failed." << endl;
      exit(0);
    }
  else 
  {
      cout << "Open Successful." <<endl;
      for(int i=0; i<raSize; i++)
      {
         file >> raAnswers[i];    
      }
      file.close(); // missing   
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
void fillCheck(char fcKey[], char fcAnswers[], bool fcCheck[], int fcSize)
{ 
  for(int w=0; w<fcSize; w++)
  {
    if(fcKey[w] == fcAnswers[w])
    {
      fcCheck[w] = "T"; // data type is bool not char write fcCheck[w]=true
    }
    else
    {
      fcCheck[w] = false; // same prob
    }
  }
}


it is a wonder that the above code compiled and if it for some mysterious way got compiled it would give unpredictable results

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int countRight(bool crCheck[], int crSize)
{
  int countRt = 0;
  
  for(int r=0; r<crSize; r++)
  {
      if (crCheck[r])
      {
         countRt++;
      }
  }

 return countRt;     
}


1
2
3
4
int findWrong(int fwCountRight, int fwSize)
{
  return fwCountRight - fwSize;
}


1
2
3
4
5
6
7
8
9
10
11
void questionsWrong(bool qwCheck[], int qwSize)
{
  
  for(int w=0; w<qwSize; w++)
  {
    if(!qwCheck[w])
    {
      cout <<(w + 1) <<" ";
    }
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char determinePass(bool dpCheck[], int dpSize)
{
  int pCount = 0;
  char determine;
  
  for(int p=0; p<dpSize; p++)
  {
    if(dpCheck[p])  pCount++;
  }
    
  if(pCount >= 15)
  {
    determine = 'P';
  }
  else 
  {
    determine = 'F';
  }
  return determine;
}


hth at bit - disclaimer, haven't compiled my changes
After making many modifications to the code, mainly those suggested before, I've found that only two problems exist now: the questionsWrong function shows more than the maximum wrong declared, and the program does not continue to read the rest of the values from the data file. I've tried to run some end of file loops on main, but nothing seems to be working and therefore I thought I'd turn to the geniuses here at cplusplus. Thanks loads in advance guys!
Last edited on
Topic archived. No new replies allowed.