按值将类型结构传递给函数,错误:“平均值”声明中存在两个或多个数据类型! -
大家好,
我有一个小问题(或者可能很大!),在编译时只有一个错误,但众所周知,一个错误就足以阻碍进展。 基本上,我对 C++ 相当陌生,并且负责编写以下代码并将类型结构参数按值传递给函数。但我收到以下错误消息: “平均值声明中有两种或多种数据类型”,因此任何针对我的一个错误的解决方案将不胜感激。 提前非常感谢...
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
int voidaverage(studentprogramming)
不是有效的语法。只有一种返回类型,即它应该是intaverage(studentprogramming)
。int void average(student programming)
is not a valid syntax. There is only one return type, i.e. it should beint average(student programming)
.看到这个了吗?
在您说明函数的返回类型的部分中,这是连续的“两种或多种类型”。下定决心吧。
您的代码还存在其他几个问题,大部分只是风格问题。你不希望你的函数声明是
extern
(因为它就在同一个文件中);你希望该声明位于 main 之外(它可以在 main 内部工作,但实际上没有意义);你不需要math.h
(无论如何它都是一个C头文件);您应该使用真正的字符串类型来表示字符串;将平均成绩存储回结构中并不是特别有用(您已经拥有它,因此只需直接使用它);并且您的几个变量名称没有任何意义(programming
是一个特别明显的例子)。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 (programming
is an especially obvious example).你对平均函数的声明应该是:
你在那里有一个额外的“void”。
Your declaration of your average function should be:
You've got an extra "void" in there.