Can't seem to pass array correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jstacey06
    New Member
    • Apr 2010
    • 1

    #1

    Can't seem to pass array correctly

    Everywhere I declare student[i].score I keep getting these error messages:

    line 46: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
    line 46: error C2228: left of '.score' must have class/struct/union
    line 56: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
    line 56: error C2228: left of '.score' must have class/struct/union
    line 58: warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    line 83: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
    line 83: error C2228: left of '.id' must have class/struct/union
    line 84: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
    line 84: error C2228: left of '.score' must have class/struct/union
    line 85: error C2676: binary '[' : 'data' does not define this operator or a conversion to a type acceptable to the predefined operator
    line 85: error C2228: left of '.grade' must have class/struct/union




    This is my program:

    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    struct data
    {
    	int id;
    	int score;
    	char grade;
    }; 
    
    
    
    float getAverage (data& student);
    void readStuData (data& student);
    void assignGrades (data student);
    void reportResults (data student);
    
    int student[10];
    
    int main ()
    {
    	data studentInfo;
    
    	readStuData (studentInfo);
    	getAverage (studentInfo);
    	assignGrades (studentInfo);
    	reportResults (studentInfo);
    
    	return 0;
    }
    
    void readStuData (data& student)
    {
    	int i;
    
    	cout << "Please enter student ID: ";
    	cin >> student.id;
    	cout <<"Enter student score: ";
    	cin >> student.score;
    
    	//Read 10 students grade information
    	for (i = 0; i < 10; i++)
    		cin >> student[i].score;
    }
    
    float getAverage (data& student)
    {
    	float avg;
    	float sum = 0.0;
    	int i;
    	
    	for (i = 0; i < 10; i++)
    		sum = student[i].score + sum;
    	
    	avg = sum/10.0;
    			
    	return avg;
    }
    
    void assignGrades (data student)
    {
    	float avg;
    
    	if ((student.score <= avg + 10) && (student.score >= avg - 10))
    		student.grade = 'S';
    	else if (student.score > (avg + 10))
    		student.grade = 'O';
    	else if (student.score < (avg - 10))
    		student.grade = 'U';
    }
    	
    void reportResults (data student)
    {
    	float avg;
    	int i;
    
    	cout << "The average is " << avg << endl;
    
    	for (int i = 0; i < 10; i++)
    		cout << setw(5) << "Student " << student[i].id << endl;
    		cout << setw(10) << "Student's score is " << student[i].score << endl;
    		cout <<	setw(15) << "Student's grade is " << student[i].grade << endl; 
    }
    Please tell me what I'm doing wrong....thanks !
    Last edited by Banfa; Apr 28 '10, 08:35 AM. Reason: Added [code]...[/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    student (data& student) is a reference structure and not an array, it can't be indexed with []. Btw, naming local variable and global array the same name will lead to confusion.

    Comment

    Working...