Hello to all that read
I have a small problem(or it could be large!), just one error at compile time but as we all know one error is all it takes to hinder progress.
Basically I am fairly new to C++ and have been tasked with writing the following code and passing by value a type stuct argument to the function. But I get the following error message:
“two or more data types in declaration of average” so any solution/s to my one error would be much appreciated.
Many thanks in advance…
enter code here
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
struct student{
char name[40];
int student_id;
int student_grades[3];
int average;
};
int main ()
{
extern int average(student);
student programming;
int j;
cout<<"\nPlease Enter the student name for student number: ";
cin>>programming.name;
cout<<"\nPlease Enter student i.d for student number: ";
cin>>programming.student_id;
cout<<"\nPlease Enter student grades for student number: ";
for(j=0;j<3;j++){
cout<<"\nEnter student grade no: "<<j+1<<"\n";
cin>>programming.student_grades[j];
}
programming.average=average(programming);
cout<<"\nNo. Name ID Number Average\n";
cout<<programming.name;
cout<<" "<<programming.student_id <<" ";
cout<<programming.average<<" ";
system ("PAUSE");
return 0;
}
struct student;
int void average(student programming){
int sum=0;
int ave=0;
int j;
for(j=0;j<3;j++){
sum=sum+programming.student_grades [j];
}
ave=sum/3;
return ave;
}
enter code here
See this?
That’s “two or more types” in a row, in the part where you say what the return type is for the function. Make up your mind.
There are several other problems with your code, mostly just stylistic. You don’t want your function declaration to be
extern(since it’s right there in the same file); you want that declaration to be outside of main (it will work inside, but there’s really no point); you don’t needmath.h(which is a C header anyway); you should be using a real string type to represent strings; storing the grade average back into the structure isn’t especially useful (you already have it, so just use it directly); and several of your variable names don’t make any sense (programmingis an especially obvious example).