C++程序中遇到问题不能运行求解答

#include<iostream.h>
#include<string.h>
#include<iomanip.h>
struct student
{
int num;
char name[10];
int score;
};
//请在下方给出search函数形式一的实现代码
void search(int x,student stu[])
{
for (int i=0;i<5;i++)
{
if(x==stu[i].num)
{
cout<<setw(5)<<stu[i].num<<setw(10)<<stu[i].name<<setw(5)<<stu[i].score<<endl;
break;
}
else
{
cout<<"No!"<<endl;
break;
}

//请在下方给出search函数形式二的实现代码
void search(char x[10],student stu[])
{
for(int i=0;i<5;i++)
{
if(strcmp(x,stu[i].name)==0)
{
cout<<setw(5)<<stu[i].num<<setw(10)<<stu[i].name<<setw(5)<<stu[i].score<<endl;
break;
}
else
{
cout<<"No!"<<endl;
break;
}
}
}

//请在下方给出search函数形式二的实现代码

void main()
{
student stu[5]={{101,"Tom",88},{102,"Marry",98},{103,"John",60},{104,"Kitty",78},{105,"Betty",90}};
int num;
char name[10];
for (int i=0;i<5;i++)
{
cout<<setw(5)<<stu[i].num<<setw(10)<<stu[i].name<<setw(5)<<stu[i].score<<endl;
}
cout<<"请输入要查找的学生的学号"<<endl;
cin>>num;
//请在下方调用search函数形式一
search(int x,student stu[]);

cout<<"请输入要查找的学生的姓名"<<endl;
cin>>name;
//请在下方调用search函数形式二
search(char x[10],student stu[]);

}
哪里错了???运行显示local function definitions are illegal 和 local function definitions are illegal

第1个回答  推荐于2016-11-15
看了一下,你这明显有2处错误
1: 第一个serach函数少2个大括号 请加在第二个 serach定义之前
2: main函数里面 是调用search 而不是定义
所以你在

//请在下方调用search函数形式一
search(int x,student stu[]);

//请在下方调用search函数形式二
search(char x[10],student stu[]);
这2处是错误的, 函数参数 你应该是传准确的参数,而不是定义
我改后,试过是可以运行的 代码如下

#include<iostream.h>
#include<string.h>
#include<iomanip.h>

struct student
{
int num;
char name[10];
int score;
};

void search(int x,student stu[]);
void search(char x[10],student stu[]);

//请在下方调用search函数形式一
void search(int x,student stu[])
{
for (int i=0;i<5;i++)
{
if(x==stu[i].num)
{
cout<<setw(5)<<stu[i].num<<setw(10)<<stu[i].name<<setw(5)<<stu[i].score<<endl;
break;
}
else
{
cout<<"No!"<<endl;
break;
}
}
}
//请在下方给出search函数形式二的实现代码
void search(char x[10],student stu[])
{
for(int i=0;i<5;i++)
{
if(strcmp(x,stu[i].name)==0)
{
cout<<setw(5)<<stu[i].num<<setw(10)<<stu[i].name<<setw(5)<<stu[i].score<<endl;
break;
}
else
{
cout<<"No!"<<endl;
break;
}
}
}

//请在下方给出search函数形式二的实现代码

void main()
{
student stu[5]={{101,"Tom",88},{102,"Marry",98},{103,"John",60},{104,"Kitty",78},{105,"Betty",90}};
int num;
char name[10];
for (int i=0;i<5;i++)
{
cout<<setw(5)<<stu[i].num<<setw(10)<<stu[i].name<<setw(5)<<stu[i].score<<endl;
}
cout<<"请输入要查找的学生的学号"<<endl;
cin>>num;
//请在下方调用search函数形式一
search(num,stu);

cout<<"请输入要查找的学生的姓名"<<endl;
cin>>name;
//请在下方调用search函数形式二
search(name,stu);

}本回答被提问者和网友采纳
第2个回答  2015-03-15
编译通过,运行不通过?追问

不是啊 编译显示与错误!

第3个回答  2015-03-15
你第一个search函数少了两个}追问

加了还是不行啊!

相似回答