### 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?.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include 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:

 ``123456789101112131415`` ``````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.
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.