我刚学了一些基本的数组主题,我想问为什么我的函数 searchID 不能用作函数。我已经检查了原型,调用函数和函数头的参数数量。你能指出我的错误吗?
#include<iostream>
using namespace std;
void getScore(int[],int,long[],int&);
void searchID(long[],int,bool&,int[]);
int main()
{
int SIZE;
long ID[SIZE],winnerID,searchID;
int score1st[SIZE],score2nd[SIZE],totalScore[SIZE], avg1st, avg2nd,count;
cout<<"Enter the number of players: ";
cin>>SIZE;
for(int i=0;i<SIZE;i++)
{
cout<<"Enter bowler "<<i+1<<"'s ID: ";
cin>>ID[i];
}
cout<<"\nFIRST ROUND SCORES: \n";
getScore(score1st,SIZE,ID,avg1st);
cout<<"\nSECOND ROUND SCORES: \n";
getScore(score2nd,SIZE,ID,avg2nd);
cout<<"\nThe player(s) who improve the scores in the second round:\n";
for(int k=0;k<SIZE;k++)
{
if(score2nd[k]>score1st[k])
{
cout<<"Player "<<k+1<<", ID "<<ID[k]<<endl;
}
}
cout<<"\nThe average score for the first round: "<<avg1st;
cout<<"\nThe average score for the second round: "<<avg2nd;
int highScore=totalScore[0];
for(int m=0;m<SIZE;m++)
{
totalScore[m]=score1st[m]+score2nd[m];
if(totalScore[m]>highScore)
{
highScore=totalScore[m];
winnerID=ID[m];
count=m+1;
}
}
cout<<"\nThe WINNER is: Player"<<count<<", ID "<<winnerID<<" with the HIGHEST score "<<highScore<<endl<<endl;
bool idFound=false;
searchID(ID,SIZE,idFound,totalScore);
while(!idFound)
{
cout<<"The data is not available!\n\n";
searchID(SIZE,ID,idFound,totalScore);
}
return 0;
}
void getScore(int score[], int size,long id[], int& avg)
{
int total=0;
for(int j=0; j<size; j++)
{
cout<<"Enter the total score for player "<<j+1<<", ID "<<id[j]<<": ";
cin>>score[j];
total=total+score[j];
}
avg=total/size;
}
void searchID(long id[],int size,bool& foundID,int total[])
{
long searchPlayer;
cout<<"Enter the ID of the player you want to search: ";
cin>>searchPlayer;
for(int n=0;n<size;n++)
{
if(searchPlayer==id[n])
{
foundID=true;
cout<<"The total score of player "<<n+1<<" with ID "<<id[n]<<" is: "<<total[n]<<endl<<endl;
}
}
}
&#13;
ID和总分都是数组。对于 getScore 函数的第一个函数,它结果完全没问题,但是当我包含第二个函数时出现了错误。
答案 0 :(得分:0)
您重新声明searchID
。
首先作为一个函数,在main()
之外,另一个作为main()
中的局部变量。
void searchID(long[],int,bool&,int[]); // function declaration
long ..., searchID; // local variable declaration in main()
块中声明的名称是 local 到该块;它有块范围。 它的潜在范围在声明点开始并以结尾处结束 它的块结束。在块作用域中声明的变量是 local 变量
因此,searchID
中最近可能的main()
声明显然是局部变量声明。因此错误。
答案 1 :(得分:0)
固定!
#include<iostream>
using namespace std;
void getScore(int[],int,long[],int&);
void searchID(long[],int,bool&,int[]);
int main()
{
int SIZE;
long ID[SIZE],winnerID;
int score1st[SIZE],score2nd[SIZE],totalScore[SIZE], avg1st, avg2nd,count;
cout<<"Enter the number of players: ";
cin>>SIZE;
for(int i=0;i<SIZE;i++)
{
cout<<"Enter bowler "<<i+1<<"'s ID: ";
cin>>ID[i];
}
cout<<"\nFIRST ROUND SCORES: \n";
getScore(score1st,SIZE,ID,avg1st);
cout<<"\nSECOND ROUND SCORES: \n";
getScore(score2nd,SIZE,ID,avg2nd);
cout<<"\nThe player(s) who improve the scores in the second round:\n";
for(int k=0;k<SIZE;k++)
{
if(score2nd[k]>score1st[k])
{
cout<<"Player "<<k+1<<", ID "<<ID[k]<<endl;
}
}
cout<<"\nThe average score for the first round: "<<avg1st;
cout<<"\nThe average score for the second round: "<<avg2nd;
int highScore=totalScore[0];
for(int m=0;m<SIZE;m++)
{
totalScore[m]=score1st[m]+score2nd[m];
if(totalScore[m]>highScore)
{
highScore=totalScore[m];
winnerID=ID[m];
count=m+1;
}
}
cout<<"\nThe WINNER is: Player"<<count<<", ID "<<winnerID<<" with the HIGHEST score "<<highScore<<endl<<endl;
bool idFound=false;
searchID(ID,SIZE,idFound,totalScore);
while(!idFound)
{
cout<<"The data is not available!\n\n";
searchID(ID,SIZE,idFound,totalScore);
}
return 0;
}
void getScore(int score[], int size,long id[], int& avg)
{
int total=0;
for(int j=0; j<size; j++)
{
cout<<"Enter the total score for player "<<j+1<<", ID "<<id[j]<<": ";
cin>>score[j];
total=total+score[j];
}
avg=total/size;
}
void searchID(long id[],int size,bool& foundID,int total[])
{
long searchPlayer;
cout<<"Enter the ID of the player you want to search: ";
cin>>searchPlayer;
for(int n=0;n<size;n++)
{
if(searchPlayer==id[n])
{
foundID=true;
cout<<"The total score of player "<<n+1<<" with ID "<<id[n]<<" is: "<<total[n]<<endl<<endl;
}
}
}
&#13;