/** readGradeFile *******************************************************
* Read the data from inf into the parallel arrays given.
* @params inf -- input file to read data from @pre inf is already open
* id[] -- container for student id #s
* asgts[] -- container for assignment grades
* mt[] -- container for mid-term grade
* exam[] -- container for exam scores
*
* @pre each student record is on a separate line of the file and
* fields are separated by one or more spaces. All fields are integers.
* All grades are expressed as percentages (0-100).
* @modifies inf -- read to end of file
* id, asgts, mt, exam -- the read information is filled in
* @returns the number of lines read (max is MAX_STUDENTS)
*/
int readGradeFile(std::ifstream& inf, int id[], int asgt[], int mt[], int exam[]);

int readGradeFile(std::ifstream& inf, int id[], int asgt[], int mt[], int exam[]){
	   int line = 0;
	   int id;
	   int asgt;
	   int mt;
	   int exam;
	   ifstream inFile;
       for(int i=1;i<=MAX_STUDENTS;i++){
            id[i] = inFile.get(id);
            asgt[i] = inFile.get(asgt);
            mt[i] = inFile.get(mt);
            exam[i] = inFile.get(exam);

       }
       return line;
}

this is my code.plese help me where should be fixed.
thanks.

inFile is declared, but there's no input stream associated with it. You need to open a file using inFile before you can use it to read from that file.

inf is passed to the function. perhaps you don't need inFile at all? Perhaps you should read from inf?

Lines 3 - 6 --> Why are you shadowing the parameters passed to the function? Delete these lines.

But i delete that ,i cannot work.....

#include"assign8.h"
#include<iostream>
#include<fstream>
using namespace std;

int readGradeFile(std::ifstream& inf, int id[], int asgt[], int mt[], int exam[]){
	   int line = 0;
       for(int i=1;i<=MAX_STUDENTS;i++){
            id[i] = cin.get(id);
            asgt[i] = cin.get(asgt);
            mt[i] = cin.get(mt);
            exam[i] = cin.get(exam);

       }
       return line;
}

double computeFinalGrades(int numStud, int asgt[], int mt[], int exam[], double final[]){
	double sum  = 0;
	int id[numStud];
	for(int i = 1;i<=numStud;i++){
		final[i] = 0.14 * asgt[i] + 0.30 * mt[i] + 0.56 * exam[i];
		cout << id[i] << "  " << final[i] <<endl;
		sum = sum + final[i];
	}
	double average = sum / numStud;
	return average;
}

void countLetterGrades(int numStud, double f[], int cnt[]){
          cnt[0] = 0;
          cnt[1] = 0;
          cnt[2] = 0;
          cnt[3] = 0;
          cnt[4] = 0;
    	  for(int j =1;j<=numStud;j++){
    		  if(f[j] >= 80){
    			  cnt[0]++;
    		  }
    		  if(f[j] < 80 && f[j]>=65){
    			  cnt[1]++;
    	      }
    		  if(f[j]>=55 && f[j] <65){
    			  cnt[2]++;
    		  }
    		  if(f[j] >=50 &&f[j] <55){
    			  cnt[3]++;
    		  }
    		  if(f[j] <50){
    			  cnt[4]++;
    		  }
      }
    	  cout << cnt[0]<<" for A's"<<endl;
    	  cout << cnt[1]<<" for B's"<<endl;
    	  cout << cnt[2]<<" for C's"<<endl;
    	  cout << cnt[3]<<" for D's"<<endl;
    	  cout << cnt[4]<<" for F's"<<endl;
}

This is my main function:

#include<iostream>
#include"assign8.h"
using namespace std;

double computeFinalGrades(int numStud, int asgt[], int mt[], int exam[], double final[]);
int readGradeFile(std::ifstream& inf, int id[], int asgt[], int mt[], int exam[]);
void countLetterGrades(int numStud, double f[], int cnt[]);

int main(){
    ifstream inData;
    inData.open("grades.txt");
	int numstudent = readGradeFile(inData,id,asgt,mt,exam);
	for(int i = 1;i<=numstudent;i++){
		double score = computeFinalGrades(numstudent,asgt,mt,exam,final);
	}
	countLetterGrades(numstudent,f,cnt);
	return 0;
}

This is the file :

1234 88 73 75
2345 90 70 80
3456 50 40 45
4567 62 51 57
5678 100 92 97
6789 85 85 85
7890 60 60 60

id[i] = cin.get(id);
            asgt[i] = cin.get(asgt);
            mt[i] = cin.get(mt);
            exam[i] = cin.get(exam);

Where did the cin come from? You have an ifstream passed to the function called inf. Use it.

id[i] = inf.get(id);
            asgt[i] = inf.get(asgt);
            mt[i] = inf.get(mt);
            exam[i] = inf.get(exam);

it comes Description Resource Path Location Type
no matching function for call to 'std::basic_ifstream<char>::get(int*&)' assign8.cpp /assign8 line 9 C/C++ Problem

int readGradeFile(std::ifstream& inf, int id[], int asgt[], int mt[], int exam[]){
	   int line = 0;
       for(int i=1;i<=MAX_STUDENTS;i++){
            id[i] = inf.get(id);
            asgt[i] = inf.get(asgt);
            mt[i] = inf.get(mt);
            exam[i] = inf.get(exam);
            
       }
       return line;
}

But how can i konw how many student are show in the txt file.how can i calculate the lines.

>> But how can i konw how many student are show in the txt file.how can i calculate the lines.

The number of lines will be the number of times through the loop. Each loop reads a line. Hence it should be a while loop, not a for loop with a pre-determined number of trips through the loop.

How do you know when you're at the end of the file? The stream will fail. How do you know when the stream fails? There are a variety of ways. One is the eof() function, though that has some pitfalls, namely that you can accidentally go through the loop one too many times if you aren't careful, so be careful

Another is that if you use the >> operator, it will fail when it tries to read past the end of the file. Same potential pitfall. You can accidentally go once too many times through the loop. Make sure you don't.

Why are you using get? This is tailor made for the >> operator.

Line 3 - Array indexes always start at 0, not 1.

Lines 4 to 7 --> read it directly into the array. No need for temp variables.

for(int i=0;i<MAX_STUDENTS;i++){
   inf >> id[i];
   inf >> asgt[i];
   inf >> mt[i];
   inf >> exam[i];
}

That's how it would be if you had MAX_STUDENTS students. Now try changing it to check for the end of file and/or the failure of the >> operator.

so my main function is correct?

int main(){
    ifstream inData;
    inData.open("grades.txt");
	int numstudent = readGradeFile(inData,id,asgt,mt,exam);
	for(int i = 1;i<=numstudent;i++){
		double score = computeFinalGrades(numstudent,asgt,mt,exam,final);
	}
	countLetterGrades(numstudent,f,cnt);
	return 0;
}

>> so my main function is correct?

You tell me. Does it compile? I see inData declared, but not id, asgt, mt, and exam. The compiler is going to give you an error if you don't declare them somewhere.

This article has been dead for over six months. Start a new discussion instead.