I have to create a program that reads in ID number and various grades from an external file and I have to place this information into an array. After all the information from the file is processed it has to compute the individual student average, then compare that with the class average to determine the grade. I am getting an error before primary expression error keeping my program from compiling and I don't understand why even after hours of google and searching this site. Here's the code
On line 59, grades is a type-name, not an object. You need to instantiate grades before accessing any members, because a class (or structure) is not an actual object, only a template for object instances. For example:
1 2 3 4 5 6 7 8 9 10 11
struct grades
{
int id[MAX_ID];
// ...
};
void process_ngrades (ifstream& ngrades)
{
grades g_; // "g_" is an instance of "grades".
g_.id[0] = // ...
}
int x; // This means "make an object of type int, called x".
x = 6;
The same is true of any other type:
1 2 3 4 5 6 7 8
double = 7.0; // doesn't work because double is a type, not an actual object
double y; // make me a double, and call it y
y = 6.0; // Now do something with that object I created
grades.stu_avg = 5.0; // doesn't work because grades is a type
grades x;
x.stu_avg = 5.0; // does work because x is an actual object
Okay I understand basically after declaring the struct type grades, I use grades as the data type for whatever object I am declaring. Here is my new code
//Use input file grades.txt to determine student grades based on information
//provided
#include<iostream>
#include<fstream>
#include<iomanip>
constint MAX_ID = 10; // max number of ids
constint MAX_SCORES = 7; // max number of scores
usingnamespace std;
struct grades
{
int id[MAX_ID]; //array for id
int scores[MAX_SCORES]; //array for scores
float stu_avg; // student average score
string grade; // student grade
};
#define in_grades "grades.txt" // defining local file grades.txt
//function prototype
void process_ngrades (ifstream&);
int main()
{
ifstream ngrades; // ngrades input stream
ngrades.open(in_grades); //open file
//if ifstream doesn't open
if (ngrades.fail())
{
cerr << "*** Cannot Open for input." << endl;
return EXIT_FAILURE;
}
process_ngrades (ngrades);
}
void process_ngrades (ifstream& ngrades)
{
int stu_score=0; //sum of student scores
int tot_score=0; //total score for all students
int tot_count=0; // count for scores
float stu_avg;
string grade; //grade for student score
float tot_avg; //total avg for entire class
float pt_diff; //point difference to determine grade
grades students; // declaring object of struct
cout << "ID "<<"Score "<< "Grade "<<endl;
while (!ngrades.eof())
{
for (int i = 0; i < MAX_ID; i++)
{
ngrades >> students.id[i];
for (int j=0; j<MAX_SCORES; j++)
{
ngrades >> students.scores[j];
stu_score += students.scores;
tot_count = MAX_SCORES;
}
students.stu_avg= stu_score / MAX_SCORES;
tot_score+= stu_score;
stu_score = 0;
}
tot_avg = tot_score/tot_count;
pt_diff = students.stu_avg - tot_avg;
if (pt_diff >= -10 && pt_diff <=10)
students.grade == "Satisfactory";
elseif (pt_diff > 10)
students.grade == "Outstanding";
else (pt_diff < -10)
students.grade == "Unsatisfactory";
cout << students.id <<" "<<students.scores <<" "<< students.grade<< endl;
}
}
Now I am getting an error on line 67 about converting from "int*" to "int", from what I've googled so far it is something about a multidimensional array
That fixed it, and just for my understanding the problem was I was trying to add the entire array to an data type int instead of just a piece of the array. I think I worded that how I meant to