Array and Funcion problem

I have this code finished as far as I can tell. It will compile. But, the output is obviously incorrect.
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
 #include <iostream>
#include <fstream>

using namespace std;

void getRscore(char[], int);
void getBscore(char[], int);
char compare(char[], char[], int);

const int SIZE=20;

int main ()
{
    int amountwrong;
    char studentanswers[SIZE];
    char correctanswers[SIZE];
    char wrong[SIZE];
    int countdex=0;
    
    
    getRscore(studentanswers, SIZE);
    
    getBscore(correctanswers, SIZE);
    
    


     
     
     
     for (int index = 0; index<SIZE; index++)
         {
         while (studentanswers[index]!=correctanswers[index])

              {
              wrong[countdex]=studentanswers[index];
              
              countdex++;}}



              
     
              cout<<"The student answers have been pulled from their file and compared to \n";
              cout<<"the correct answers. The amount of answers that were wrong is: " <<countdex <<endl;
              cout<<"The incorrect answers are as follows: ";
              for (int count=0; count<countdex; count++)
          cout<<wrong[count];             
              
 
              
                                            
    
 
 
 
 
 
 
 
 
 
 
 
 
 
     system ("pause");
    return 0;
}

void getRscore(char score[], int SIZE)

{
     ifstream Answers;
     
     Answers.open("StudentAnswers.txt");
    char answers[SIZE];
    int count=0;
    while (count<SIZE && Answers >> answers[count])
     count++;
     
     Answers.close();

}



void getBscore(char Score[], int SIZE)

{
     ifstream answers;
     
     answers.open("CorrectAnswers.txt");
     char swers[SIZE];
     int count=0;
     while (count<SIZE && answers >> swers[count])
     count++;
     
     answers.close();
     
}






















            
I'm just going to take a stab at this. Within your functions, make score[] and Score[] into pointers (*score[] *Score[]). This way you can put the values into the arrays that you want the answers to be in. And get rid of answers[] and swers[]. In the while loops, also make sure that they are pointers as well (answers[count] would be *score[count]). Not 100% sure if this is what you do but I'm pretty sure it is.
We have not studied that in our class and I will not be allowed to use "pointers"
In your getRscore function you don't check to make sure your file opened successfully. Its possible that you are not inputting the data properly. Also you store the ansers in a local array ansers[]. You do not pass this array back to main so when the function ends the array is destroyed along with the answers. you should be inputting the data into your score[] array. You have similar problems with your getBscore function and I don't see a compare function definition or function call at all.
Last edited on


It still doesn't work right. I think I have fixed what Yanson said to.













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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <fstream>

using namespace std;

void getRscore(char[], int);
void getBscore(char[], int);
char compare(char[], char[], int);

const int SIZE=20;

int main ()
{
    int amountwrong;
    char studentanswers[SIZE];
    char correctanswers[SIZE];
    char wrong[SIZE];
    int countdex=0;
    
    
    getRscore(studentanswers, SIZE);
    
    getBscore(correctanswers, SIZE);
    
    


     
     
     
     for (int index = 0; index<SIZE; index++)
         {
         while (studentanswers[index]!=correctanswers[index])

              {
              wrong[countdex]=studentanswers[index];
              
              countdex++;}}



              
     
              cout<<"The student answers have been pulled from their file and compared to \n";
              cout<<"the correct answers. The amount of answers that were wrong is: " <<countdex <<endl;
              cout<<"The incorrect answers are as follows: ";
              for (int count=0; count<countdex; count++)
          cout<<wrong[count];             
              
 
              
                                            
    
 
 
 
 
 
 
 
 
 
 
 
 
 
     system ("pause");
    return 0;
}

void getRscore(char score[], int SIZE)

{
     ifstream Answers;
     
     Answers.open("StudentAnswers.txt");
    char answers[20];
    int count=0;
    while (count<20 && Answers)
     Answers>>score[count];
     count++;
     
     Answers.close();
 for(int index=0; index<20; index++)
  cout<<answers[index] <<" ";
}



void getBscore(char Score[], int SIZE)

{
     ifstream answers;
     
     answers.open("CorrectAnswers.txt");
     
     int count=0;
     while (count<SIZE && answers >> Score[count])
     count++;
     
     answers.close();
     
}






















            
Topic archived. No new replies allowed.