Reputation: 19
My code is to get data for football players from user. I have written the first part but I am having difficulties running it as compiler raises "use of undeclared identifier" error for "enterplayer()" in main switch but I declared in the header file and included the header. Could you please take a look? Thanks
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#ifndef HW05F_H
#define HW05F_H
#endif
using namespace std;
struct fbplayer{
string name, position;
int scores, catches, yards, ryards, rushyard;
};
// Declaring array for storing football players
struct fbplayer player[10];
// declaring number of players
int numberplayers = 10;
int main(){
int option;
//Iterate till user want to exit
while (1)
{
//print the menu
cout << "Menu:\n1.Read the player's data. \n2.Print the player's data. "
<<"\n3.Update the data. \n4.Search the data. \n5.Exit \n";
//read the option
cout << "\nEnter your option: ";
cin >> option;
switch (option)
{
case 1:
//read the data
enterplayer();
break;
}
}
}
hw05f2.cpp
#ifndef HW05F_H
#define HW05F_H
#include "main.cpp"
#endif
using namespace std;
void enterplayer(fbplayer *player){
for (int i = 0; i<numberplayers; i++) {
cout<< "\nEnter player details here ";
cout<< "\nEnter player name ";
cin >> player[i].name;
cout << "Enter the position.";
cin >> player[i].position;
cout << "Enter scores of the player";
cin >> player[i].scores;
cout << "Enter catches of the player";
cin >> player[i].catches;
cout << "Enter passing yards of the player";
cin >> player[i].yards;
cout << "Enter receiving yards of the player";
cin >> player[i].ryards;
cout << "Enter rushing yards of the player";
cin >> player[i].rushyard;
}
}
hw05f.h
#ifndef hw05f_h
#define hw05f_h
#include "hw05f2.cpp"
void enterplayer(fbplayer *player);
#endif /* hw05f_h */
Upvotes: 0
Views: 1411
Reputation: 238311
When I try to run my c++ program it says “use of undeclared identifier” but it declared in header
You don't include the header into main.cpp, so it doesn't matter whether you declare the identifier there or not. What matters is whether you've declared the identifier where you use it (either directly or through inclusion).
Upvotes: 2