Help!!!


Hi,
this program should finds the frequency of each grade in the file,
here is my code, but it gives me all same value which is 8 and it's wrong!
what did I do wrong here?.


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

int main()
{

	ifstream infile;
	infile.open("info.txt");

	string IDs[10];
	char grade[10];
	int gradecount[5] = {0,0,0,0,0};
	char x;

	for(int i=0;i<10;i++)
	{
		infile >> IDs[i] >> grade[i];
		x = grade[i];
		cout << IDs[i] <<"   " << grade[i] << endl;
	}

	while(infile)
	{
		
		for(int i=0; i<5;i++)
		{   
			if(x =='A')
			   gradecount[i]++;
			else if(x == 'B')
				gradecount[i]++;
			else if(x == 'C')
				gradecount[i]++;
			else if(x == 'D')
				gradecount[i]++;
			else if(x == 'F')
				gradecount[i]++;

				infile >> grade[i];
		}	
		
	}

	cout << "Grade" << "     " << "Count" << endl;
	cout << 'A' << "  :   " << gradecount[0] << endl;
	cout << 'B' << "  :   " << gradecount[1] << endl;
	cout << 'C' << "  :   " << gradecount[2] << endl;
	cout << 'D' << "  :   " << gradecount[3] << endl;
	cout << 'F' << "  :   " << gradecount[4] << endl;

	cout << endl;
	return 0;
}
Last edited on
First things first, your "gradecount[]" array only has 6 places yet you are going up to "gradecount[10]" in that for loop starting at Line 27, so even if you get this to work it's flawed at that point.

I've changed it to 5 ,but still it gives the same value and it's wrong !!!
From what I can tell of your output, what you want is to increament gradecount[0] when x is equal to 'A', gradecount[1] when x is equal to 'B' etc. That's not what you're doing though, you are increamenting the integer held at the position of the loop counter by one for every pass. EDIT: This still shouldn't give you 8 so I'm still looking at this.

This would be SO MUCH EASIER if teachers taught objects first in an object oriented language!! AAAARRRGGGHHHH!!! </rage>

REEDIT: Please post your source text file so that I have a more complete picture.
Last edited on
x isn't changing before your for loop starts at Line 27. So at every pass you're checking the same value.

EDIT: AARRGGHH! Do you mind starting over? Here is a basic layout:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
     ifstream infile;
                   infile.open("info.txt");

     string IDs; /*What's this for?*/
     char grade; /*No array needed here*/
     int gradecounter[] = {0, 0, 0, 0, 0}; /*Size of the array is implied*/


    while(infile >> IDs >> grade)
        {
            if(/*"grade" is some letter*/)
                    {gradecounter[0] = gradecounter[0] + 1;}/*gradecounter[0] is an A*/
/*The rest is up to you*/
Last edited on

here is the Question :
Assume that enum Grade = {A, B, C, D, F} is an enumerated data type that represents grades in a course. Write a C++ program that reads a list of students ID’s and grades, finds the frequency of each grade in the file, and stores that frequency in an array of 5 integers. Use the Grade data type to loop through the array.
Ok, that's more clear. Are you provided with a "Grade data type" or are you expected to write it out yourself?

I have to write it by my self
If you give it a shot I can critique your code, the rules on this forum implicitly forbid me from handing over answers. So write up what you think the class should look like and I'll tell you if it would work for this task.
IDs : grade
u0888 A
u0777 B
u0999 D
u0444 F
u0333 B
u0444 C
u0777 B
u0898 D
u0222 F
u0777 A
Topic archived. No new replies allowed.