0

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 */
MeXoX
  • 19
  • 3
  • 4
    Why are you including .cpp files? And `enterplayer()` is declared in a header file which is not included in main.cpp I think you should start learning the basics about includes in c++ – derpirscher Oct 18 '20 at 14:10
  • Related: https://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header – Nate Eldredge Oct 18 '20 at 14:10
  • Do not repeat the identifier used in an [include guard](https://en.wikipedia.org/wiki/Include_guard). They MUST be unique. – user4581301 Oct 18 '20 at 14:12
  • 2
    Recommendation: Practice with some simpler examples for a while. Your code shows significant misunderstandings in the purpose and use of headers. – user4581301 Oct 18 '20 at 14:19
  • what to do you guys recommend me to practice? – MeXoX Oct 18 '20 at 15:15
  • Depends a bit on the textbook you're learning from. Find the section on headers and multiple file programs, read through it, and work though the example s and problems in the text. – user4581301 Oct 18 '20 at 18:23

1 Answers1

2

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).

eerorika
  • 232,697
  • 12
  • 197
  • 326