i need help trying to infile grades to an array and grade distribution

I've been trying to get this program to run but I have been having trouble infiling the grades from a .txt file. I also have a few other errors in my Calculations and calcGradeDistrib function...I've been working on this for a few hours so any help you guys could give me would be greatly appreciated...

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
133
134
135
136
137
138
139
140
141
142
143
  /*
You are to create a file called "Grades.txt". You should put 20 random grades of your choosing in there (manually, not the computer generated ones).

Your program should then inFile all the grades from the file in a function called getGrades().  //the return type and arguements are up to you to 
figure out.

Once you are done with that function, your program should go to another function called Calculations().  This function will receive the array of 
grades and calculate the smallest, largest, and average of the numbers.  **do not sort the numbers**.

After that function is done, your program should go to the final function called calcGradeDistrib().  This function will receive the array and 
calculate how many A's, B's, etc. there are. 

Be sure to display the results of the 2nd and 3rd funcion to the screen in a nice fashion.
*/

 #include<iostream>
 #include<iomanip>
 #include<string>
 #include<fstream>
 using namespace std;

 void getGrades(int[], int);
 void Calculations(int[],int);
 void calcGradeDistrib(int[]&,int);

 int main()
 {
 const int SIZE=20;
 int grades[SIZE];
 int size=0;


 Calculations(grades,size);
 calcGradeDistrib(grades&, SIZE);



 system("pause");
 return 0;

 }

 //this function infiles 20 grades from a .txt file
 void getGrades(int grades[], int size)
{
 ifstream infile;
 infile.open("Grades.txt");

 for (int x=0; x<20; x++)
 {
 infile>>grades[x];
 }

 if(!infile)
 {
	 cout<<"cannot open file"<<endl;
 }
 infile.close();
 
}


 //this function calculates the lowest, highest, and average grade
 void Calculations(int grades[],int size)
 {
 int lowest=grades[0];
 for(int count=1; count<size; count++)
 {
 if(grades[count]<lowest)
 lowest=grades[count];
 }
 cout<<lowest<<" is the lowest grade. "<<endl;
 }  

 {
 int highest=grades[0];
 for(int count=1; count<size; count++)
 {
 if(grades[count]>highest)
 highest=grades[count];
 }
 cout<<highest<<" is the highest grade. "<<endl;
 }

 {
 double average=0.0;
 int total=0;
 for(int count=0;count<size; count++)
 {
 total+=grades[count];
 }
 average=total/size;
 cout<<average<<" is the average grade. "<<endl;
 }

 
 
 
 //this function calculates the letter grades
 void calcGradeDistrib(int grades[]&,int size)
 {
 int distribution[5]={0,0,0,0,0};
 int total=0;
 for(int x=0; x<20; x++)
 {
 if(grades[x]>=50&&grades[x]<=59)
 {
 total+=total;
 total++;
 distribution[0]=total;
 }
 else if(grades[x]>=60&&grades[x]<=69)
 {
 total+=total;
 total++;
 distribution[1]=total;
 }
 else if(grades[x]>=70&&grades[x]<=79)
 {
 total+=total;
 total++;
 distribution[2]=total;
 }
 else if(grades[x]>=80&&grades[x]<=89)
 {
 total+=total;
 total++;
 distribution[3]=total;
 }
 else
 {
 total+=total;
 total++;
 distribution[4]=total;
 }

 }
 cout<<distribution[0]<<endl;
 cout<<distribution[1]<<endl;
 cout<<distribution[2]<<endl;
 cout<<distribution[3]<<endl;
 cout<<distribution[4]<<endl;
 }

Thanks in advance for your help!!!
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//this function infiles 20 grades from a .txt file
 void getGrades(int grades[], int size)///you are not using parameter size anywhere
{
 ifstream infile;
 infile.open("Grades.txt");

 ///always check if your stream is valid before any attempt to use it
/// i mean the reverse of what you've done, because your if(!infile) might always be true

 for (int x=0; x<20; x++)
 {
 infile>>grades[x];
 }

 if(!infile)/// check before use, at this point, this condition will always be true because
 {          ///  infile is at EOF
	 cout<<"cannot open file"<<endl;
 }
 infile.close();
 
}
Last edited on
Thanks for replying...I am very confused as to what I should put in my parameters...Are you saying I should move this part of the code after I open the file?
1
2
3
4
5
  
 if(!infile
 {          
	 cout<<"cannot open file"<<endl;
 }   


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
void getGrades(int grades[], int size)
{
 ifstream infile;
 infile.open("Grades.txt");
 
 if(!infile)
 {
	 cout<<"cannot open file"<<endl;
 }
 for (int x=0; x<20; x++)
 {
 infile>>grades[x];
 }


 infile.close();
 
}
closed account (SECMoG1T)
Yea that's is exactly what you should do, also it is better to use your stream to control your loop , think of this , what if the stream encountered some erroneous non numeric characters in your file? what if by mistake we forgot to add exactly 20 grades into the text file so that we end up with say 18 grades, what would happen?

good point...How do I stream control my loop?
I know I should put in a .eof some where but I am not sure where.. (This is my first time infiling more than one number)
closed account (SECMoG1T)
OG Please forget about EOF completely , the stream itself have a type conversion operator that can convert a stream object a bool based on it's current state, that's if the stream is in an error state the stream operator will return false, if the stream is fine the operator will return true, i would recommend that you use the stream directly in the condition rather that using EOF because your stream might encounter errors other that EOF.

for (int x=0; x<20&&infile; x++) //something like that could do though a while
// loop might be better
like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 //this function infiles 20 grades from a .txt file
 void getGrades(int grades[], int size)
{
 ifstream infile;
 infile.open("Grades.txt");
 
 if(!infile)
 {
	 cout<<"cannot open file"<<endl;
 }
 
 for (int x=0; x<20&&infile; x++)
 {
 infile>>grades[x];
 }
  
 infile.close();
 
}


How would I pass the values to other functions? Do I just call the getGrades function inside my other functions or does it automatically transfer?
closed account (SECMoG1T)
How would I pass the values to other functions? which values are you talking about?
I need to pass all the values I infiled into the array to my Calculations function....
These are the instructions given to me.........
Once you are done with that function, your program should go to another function called Calculations(). This function will receive the array of
grades and calculate the smallest, largest, and average of the numbers. **do not sort the numbers**.
closed account (SECMoG1T)
Well that one would be based on your preference but the best might be to have independent functions that are called individually so maybe you might find it easier to pass your array as a parameter to your functions.
would this do it?


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
//this function infiles 20 grades from a .txt file
 void getGrades(int grades[], int size)
{
 ifstream infile;
 infile.open("Grades.txt");

 for (int x=0; x<20; x++)
 {
 infile>>grades[x];
 }

 if(!infile)
 {
	 cout<<"cannot open file"<<endl;
 }
 infile.close();
 
}


 //this function calculates the lowest, highest, and average grade
 void Calculations(int grades[],int size)
 {

void getGrades(grades, size);
 int lowest=grades[0];
 for(int count=1; count<size; count++)
 {
 if(grades[count]<lowest)
 lowest=grades[count];
 }
 cout<<lowest<<" is the lowest grade. "<<endl;
 }  

 {
 int highest=grades[0];
 for(int count=1; count<size; count++)
 {
 if(grades[count]>highest)
 highest=grades[count];
 }
 cout<<highest<<" is the highest grade. "<<endl;
 }

 {
 double average=0.0;
 int total=0;
 for(int count=0;count<size; count++)
 {
 total+=grades[count];
 }
 average=total/size;
 cout<<average<<" is the average grade. "<<endl;
 }
closed account (SECMoG1T)
Why did you ignore my advice here
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream infile;
 infile.open("Grades.txt");

 for (int x=0; x<20; x++)
 {
 infile>>grades[x]; 
 }
                                       ^
 if(!infile)//?????              |
 {
	 cout<<"cannot open file"<<endl;
 }
 infile.close();


why not just call them like this

1. read grades to an array in the first function
2. pass the array to your calculate function
3. then pass it to your {><?:* function
Last edited on
Sorry I meant to post this code but I copied and pasted from my first post...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//this function infiles 20 grades from a .txt file
 void getGrades(int grades[], int size)
{
 ifstream infile;
 infile.open("Grades.txt");
 
 if(!infile)
 {
	 cout<<"cannot open file"<<endl;
 }
 
 for (int x=0; x<20&&infile; x++)
 {
 infile>>grades[x];
 }
  
 infile.close();
 
}


So you're saying just pass the array over to the other functions as a parameter...Will my grades [] variable get the job done?
this is my fully updated code but I keep getting these errors in my Calculations function

.cpp(77): error C2447: '{' : missing function header (old-style formal list?)
.cpp(89): error C2447: '{' : missing function header (old-style formal list?)

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
You are to create a file called "Grades.txt". You should put 20 random grades of your choosing in there (manually, not the computer generated ones).

Your program should then inFile all the grades from the file in a function called getGrades().  //the return type and arguements are up to you to 
figure out.

Once you are done with that function, your program should go to another function called Calculations().  This function will receive the array of 
grades and calculate the smallest, largest, and average of the numbers.  **do not sort the numbers**.

After that function is done, your program should go to the final function called calcGradeDistrib().  This function will receive the array and 
calculate how many A's, B's, etc. there are. 

Be sure to display the results of the 2nd and 3rd funcion to the screen in a nice fashion.
*/

 #include<iostream>
 #include<iomanip>
 #include<string>
 #include<fstream>
 


using namespace std;

 void getGrades(int[], int);
 void Calculations(int[],int);
 void calcGradeDistrib(int[],int);

 int main()
 {
 const int SIZE=20;
 int grades[SIZE];


 Calculations(grades,SIZE);
 calcGradeDistrib(grades, SIZE);


 system("pause");
 return 0;

 }

 //this function infiles 20 grades from a .txt file
 void getGrades(int grades[], int SIZE)
{
 ifstream infile;
 infile.open("Grades.txt");
 
 if(!infile)
 {
	 cout<<"cannot open file"<<endl;
 }
 
 for (int x=0; x<20&&infile; x++)
 {
 infile>>grades[x];
 }
  
 infile.close();
 
}


 //this function calculates the lowest, highest, and average grade
 void Calculations(int grades[],int SIZE)
 {
 int lowest=grades[0];
 for(int count=1; count<SIZE; count++)
 {
 if(grades[count]<lowest)
 lowest=grades[count];
 }
 cout<<lowest<<" is the lowest grade. "<<endl;
 }  

 {
 
 int highest=grades[0];

 for(int count=1; count<SIZE; count++)
 {
 if(grades[count]>highest)
 highest=grades[count];
 }
 cout<<highest<<" is the highest grade. "<<endl;
 }

 {
 double average=0.0;
 int total=0;
 for(int count=0;count<SIZE; count++)
 {
 total+=grades[count];
 }
 average=total/SIZE;
 cout<<average<<" is the average grade. "<<endl;
 }

 
 
 
 //this function calculates the letter grades
 void calcGradeDistrib(int grades[],int SIZE)
 {
 int distribution[5]={0,0,0,0,0};
 int total=0;
 for(int x=0; x<20; x++)
 {
 if(grades[x]>=50&&grades[x]<=59)
 {
 total+=total;
 total++;
 distribution[0]=total;
 }
 else if(grades[x]>=60&&grades[x]<=69)
 {
 total+=total;
 total++;
 distribution[1]=total;
 }
 else if(grades[x]>=70&&grades[x]<=79)
 {
 total+=total;
 total++;
 distribution[2]=total;
 }
 else if(grades[x]>=80&&grades[x]<=89)
 {
 total+=total;
 total++;
 distribution[3]=total;
 }
 else
 {
 total+=total;
 total++;
 distribution[4]=total;
 }

 }
 cout<<distribution[0]<<endl;
 cout<<distribution[1]<<endl;
 cout<<distribution[2]<<endl;
 cout<<distribution[3]<<endl;
 cout<<distribution[4]<<endl;
 }


Last edited on
closed account (SECMoG1T)
I din't see anywhere you called this function getGrades(int[], int);

also when you print something you need to tell the user what it is otherwise he/she will get confused e.g. you lines 142-146 what are those values that will get printed? the user will not know.
Error handling and reporting is difficult and it seems like it's never covered in beginner classes. It's a good idea to have your functions return success/failure of some sort. Here's a good way to do getGrades()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Read up to "size" grades from "Grades.txt" into the grades array.
// Return the number of grades actually read, or -1 to indicate an error
int getGrades(int grades[], int size)
{
    ifstream infile;
    infile.open("Grades.txt");
    int x=0;
    while (x<20 && (infile>>grades[x])) {
        ++x;
    }
    if (infile || infile.eof()) {
        return x;
    } else {
        return -1;
    }
}


Then the main program is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int
main()
{
    const int SIZE = 20;
    int grades[SIZE];

    int size = getGrades(grades, SIZE);
    if (size < 0) {
        cout << "Error reading grades\n";
        return 1;
    } else if (size == 0) {
        cout << "No grades read\n";
        return 1;
    } else {
        cout << "Read " << size << " grades\n";
    }

    Calculations(grades, size); // pass actual number of grades
    calcGradeDistrib(grades, size); // pass actual number of grades
    return 0;
}


I've added the call to getGrades(), which you didn't have anywhere.

As for your current problems, if you indent the code properly, you'll see the problem immediately:
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
 // this function calculates the lowest, highest, and average grade
void
Calculations(int grades[], int SIZE)
{
    int lowest = grades[0];
    for (int count = 1; count < SIZE; count++) {
        if (grades[count] < lowest)
            lowest = grades[count];
    }
    cout << lowest << " is the lowest grade. " << endl;
}

// Looks like the start of a new function
{

    int highest = grades[0];

    for (int count = 1; count < SIZE; count++) {
        if (grades[count] > highest)
            highest = grades[count];
    }
    cout << highest << " is the highest grade. " << endl;
}

// Looks like the start of a new function
{
    double average = 0.0;
    int total = 0;
    for (int count = 0; count < SIZE; count++) {
        total += grades[count];
    }
    average = total / SIZE;
    cout << average << " is the average grade. " << endl;
}

Just delete lines 11-14 and 23-26 in my listing

Your CalcDistribution isn't right. The loop just needs to increment the appropriate distribution value:
1
2
3
4
5
6
7
8
void
calcGradeDistrib(int grades[], int SIZE)
{
    int distribution[5] = { 0, 0, 0, 0, 0 };
    for (int x = 0; x < 20; x++) {
        if (grades[x] >= 50 && grades[x] <= 59) {
            ++distribution[0];
        } // etc 


Also, you need to print the number of letter grades at the end, not just the numbers. For example:
cout << distribution[0] << "F's" << endl;
This is my updated code... As far as my Calculations function goes, I need to calculate the lowest, highest, and average grade in the same function without sorting. I am have an idea as to how to do that separately but I am trying to figure out how to do it in the same function..
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
You are to create a file called "Grades.txt". You should put 20 random grades of your choosing in there (manually, not the computer generated ones).

Your program should then inFile all the grades from the file in a function called getGrades().  //the return type and arguements are up to you to 
figure out.

Once you are done with that function, your program should go to another function called Calculations().  This function will receive the array of 
grades and calculate the smallest, largest, and average of the numbers.  **do not sort the numbers**.

After that function is done, your program should go to the final function called calcGradeDistrib().  This function will receive the array and 
calculate how many A's, B's, etc. there are. 

Be sure to display the results of the 2nd and 3rd funcion to the screen in a nice fashion.
*/

 #include<iostream>
 #include<iomanip>
 #include<string>
 #include<fstream>
 
 int getGrades(int[], int);
 void Calculations(int[],int);
 void calcGradeDistrib(int[],int);

using namespace std;

 

 int main()
{
    const int SIZE = 20;
    int grades[SIZE];

    int size = getGrades(grades, SIZE);
    if (size < 0) {
        cout << "Error reading grades\n";
        return 1;
    } else if (size == 0) {
        cout << "No grades read\n";
        return 1;
    } else {
        cout << "Read " << size << " grades\n";
    }

    Calculations(grades, size); // pass actual number of grades
    calcGradeDistrib(grades, size); // pass actual number of grades
    return 0;
}


// Read up to "size" grades from "Grades.txt" into the grades array.
// Return the number of grades actually read, or -1 to indicate an error
int getGrades(int grades[], int size)
{
    ifstream infile;
    infile.open("Grades.txt");
    int x=0;
    while (x<20 && (infile>>grades[x])) {
        ++x;
    }
    if (infile || infile.eof()) {
        return x;
    } else {
        return -1;
    }
}



 //this function calculates the lowest, highest, and average grade
 void Calculations(int grades[],int SIZE)
 {
 int lowest=grades[0];
 for(int count=1; count<SIZE; count++)
 {
 if(grades[count]<lowest)
 lowest=grades[count];
 }
 cout<<lowest<<" is the lowest grade. "<<endl;
 }  

 {
 
 int highest=grades[0];

 for(int count=1; count<SIZE; count++)
 {
 if(grades[count]>highest)
 highest=grades[count];
 }
 cout<<highest<<" is the highest grade. "<<endl;
 }

 {
 double average=0.0;
 int total=0;
 for(int count=0;count<SIZE; count++)
 {
 total+=grades[count];
 }
 average=total/SIZE;
 cout<<average<<" is the average grade. "<<endl;
 }

 
 
 
 //this function calculates the letter grades
 void calcGradeDistrib(int grades[],int SIZE)
 {
 int distribution[5]={0,0,0,0,0};
 int total=0;
 

 for(int x=0; x<20; x++)
 {
 
	 if(grades[x]>=50&&grades[x]<=59)
 {
 total+=total;
 total++;
 distribution[0]=total;
 }
 
	 else if(grades[x]>=60&&grades[x]<=69)
 {
 total+=total;
 total++;
 distribution[1]=total;
 }
 
	 else if(grades[x]>=70&&grades[x]<=79)
 {
 total+=total;
 total++;
 distribution[2]=total;
 }
 
	 else if(grades[x]>=80&&grades[x]<=89)
 {
 total+=total;
 total++;
 distribution[3]=total;
 }
 
	 else
 {
 total+=total;
 total++;
 distribution[4]=total;
 }

 }
 cout<<distribution[0]<< " of the grades are Fs"<<endl;
 cout<<distribution[1]<<" of the grades are Ds"<<endl;
 cout<<distribution[2]<<" of the grades are Cs"<<endl;
 cout<<distribution[3]<<" of the grades are Bs"<<endl;
 cout<<distribution[4]<<" of the grades are As"<<endl;
 }





Last edited on
got it working...Thank you for all your help
Topic archived. No new replies allowed.