Writing an array to an outfile

Pages: 12
The display works but it's not pulling the information from the scores.txt so I don't know if it's not pulling from the right place or a formatting problem or something.


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
127
128
129
130
131
132
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int maxCategory = 7;
int getCategory(int);
void initialize(int student[]);
void getStudent(int category, int student[]);
void printArray(int category, int student[]);

int main()
{
int category;
int score;
int student[maxCategory];
ifstream infile;
infile.open("scores.txt");

if (!infile)
{
cout<<"Cannot open input file. "<< endl;
return 1;
}

initialize(student);
infile >> score;

while (!infile.eof())
{
category = getCategory(score);
getStudent(category, student);

infile >> score;
}

infile.close();
printArray(category, student);

system("Pause");
return 0;
}

void initialize(int student[])
{
int j;
for (j=0; j<= maxCategory; j++)
{
student[j]=0;
}
}

void getStudent(int c, int s[])
{
if (c <= maxCategory)
   {
s++;      
   }
}

void printArray(int category, int student[])
{
	string scoreRange;
	cout << "  Score Range #of students" << endl;

	for (category=0; category <= maxCategory; category++)
	  {
		  switch (category)
		  {
		  case 0:
			  scoreRange= "      0----24";
			  break;
                            case 1:
			  scoreRange= "      25---49";
			  break;
	               case 2:
			  scoreRange= "      50---74";
			  break;
		  case 3:
			  scoreRange= "      75---99";
              break;	                                                                                                                       case 4:
			  scoreRange= "      100-124";
			  break;
		  case 5:
			  scoreRange= "      125-149";
			  break;
		  case 6:
			  scoreRange= "      150-174";
			  break;
		  case 7:
			  scoreRange= "      175-200";
			  break;
		  }

	     cout << scoreRange << "  ";
	      cout << student[category ]<< endl;	  
      }
}

int getCategory(int score)
{
    int scoreRange;

    if (score >=0 && score <25)
	scoreRange=0;
	
   else if (score >=25 && score <50)
	scoreRange=1;
	
   else if (score >=50 && score <75)
	scoreRange=2;

   else if (score >=75 && score < 100)
	scoreRange=3;

   else if (score >=100 && score <125)
	scoreRange=4;

   else if (score >=125 && score <150)
	scoreRange=5;

   else if (score >=150 && score <175)
	scoreRange=6;

   else if (score >=175 && score <=200)
	scoreRange=7;

   
     return scoreRange;
}
on line 67:
for (category=0; category <= maxCategory; category++)

aren't you resetting the value of category to 0

also in your main() you have a student array but are you actually using and filling the array right?
Last edited on
Topic archived. No new replies allowed.
Pages: 12