Struct and error before primary expression
Jul 8, 2012 at 9:36pm Jul 8, 2012 at 9:36pm UTC
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
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
//Use input file grades.txt to determine student grades based on information
//provided
#include<iostream>
#include<fstream>
#include<iomanip>
const int MAX_ID = 10; // max number of ids
const int MAX_SCORES = 7; // max number of scores
using namespace 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
cout << "ID " <<"Score " << "Grade " <<endl;
while (!ngrades.eof())
{
for (int i = 0; i < MAX_ID; i++)
{
ngrades >> grades.id[i];
for (int j=0; j<MAX_SCORES; j++)
{
ngrades >> grades.scores[j];
stu_score += grades.scores;
tot_count = MAX_SCORES;
}
grades.stu_avg= stu_score / MAX_SCORES;
tot_score+= stu_score;
stu_score = 0;
}
tot_avg = tot_score/tot_count;
pt_diff = grades.stu_avg - tot_avg;
if (pt_diff >= -10 && pt_diff <=10)
grades.grade == "Satisfactory" ;
else if (pt_diff > 10)
grades.grade == "Outstanding" ;
else (pt_diff < -10)
grades.grade == "Unsatisfactory" ;
cout << grades.id <<" " <<grades.scores <<" " << grades.grade<< endl;
}
}
And the error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/d_compsc
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/chpt9.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/chpt9.o.d -o build/Debug/GNU-MacOSX/chpt9.o chpt9.cpp
chpt9.cpp: In function 'void process_ngrades(std::ifstream&)' :
chpt9.cpp:61: error: expected primary-expression before '.' token
chpt9.cpp:65: error: expected primary-expression before '.' token
chpt9.cpp:66: error: expected primary-expression before '.' token
chpt9.cpp:69: error: expected unqualified-id before '.' token
chpt9.cpp:74: error: expected primary-expression before '.' token
chpt9.cpp:77: error: expected unqualified-id before '.' token
chpt9.cpp:79: error: expected unqualified-id before '.' token
chpt9.cpp:81: error: expected `;' before ' grades'
chpt9.cpp:83: error: expected primary-expression before ' .' token
chpt9.cpp:83: error: expected primary-expression before ' .' token
chpt9.cpp:83: error: expected primary-expression before ' .' token
make[2]: *** [build/Debug/GNU-MacOSX/chpt9.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 464ms)
Jul 8, 2012 at 9:44pm Jul 8, 2012 at 9:44pm UTC
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] = // ...
}
Wazzak
Jul 8, 2012 at 9:45pm Jul 8, 2012 at 9:45pm UTC
Consider a class as the blueprints of a building.
You are trying to live in the blueprint.
Jul 8, 2012 at 9:52pm Jul 8, 2012 at 9:52pm UTC
Do you undstand why this doesn't work:
int = 6;
and this does work
1 2
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
Jul 8, 2012 at 10:50pm Jul 8, 2012 at 10:50pm UTC
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
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
//Use input file grades.txt to determine student grades based on information
//provided
#include<iostream>
#include<fstream>
#include<iomanip>
const int MAX_ID = 10; // max number of ids
const int MAX_SCORES = 7; // max number of scores
using namespace 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" ;
else if (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
Jul 8, 2012 at 10:54pm Jul 8, 2012 at 10:54pm UTC
stu_score += students.scores;
stu_score
is an int.
students.scores
is an array of int
s.
You're trying to add an array to an int. You can't do that. Did you mean something like:
stu_score += students.scores[j];
?
Jul 8, 2012 at 11:07pm Jul 8, 2012 at 11:07pm UTC
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
Topic archived. No new replies allowed.