0

So I'm having trouble understanding how Struct works in C++ I have develop a code in which I've been playing for a while but I don't seem to display for the results I'm looking for. I don't get any compiler errors or mistakes so it seems to be running ,This is what I have ...

Question: How do I display the results in "void Save_Player_Name(Player_Data Player)" later on in the future... ?

struct Player_Data
{
public: string Player_Name;// name of the player will be store here
}Customer[1];

int main()

{

Save_Name_File();

}





void Save_Name_File()// will capture the name of the player
{

    int n;
    int i = 1;// number of players
    //cin.get();

    for (n=0; n<i; n++)// will the player
    {
     cout << string(30, '\n');
     cout << "Player Amount " << n << " Out of : " << i;

    cout << "\n Please enter the name you wish to play \n\n Name: ";
    getline (cin,Customer[n].Player_Name);


    }



}

void Save_Player_Name(Player_Data Player)// will store the name of the player in a file
{

      ofstream scores_data;
      scores_data.open ("scores.dat", std::ios_base::app);
      cout << Player.Player_Name << endl;
      scores_data<< Player.Player_Name << "\n";
      scores_data.close();

}

2 Answers 2

1

Edit: minor fixes.
Edit: Added class consideration.

Question: How do I display the results in "void Save_Player_Name(Player_Data Player)" later on in the future... ?

If you are asking how to read the data in from a file:

const bool readFile()
{
   ifstream ip;
   ip.open("scores.dat", ifstream::in);
   if( !ip )
   {
      printf("Unable to open file.");
      return false;
   }

   // loop over every line in the file
   string bffr;
   while( getline(ip, bffr) )
   {
      <do something>
   }
}

If you are referring to how to access the data stored in the variable: Technically, you should be able to do the following from main:

Save_NameFile();
printf("Player name: %s", Customer[n].Player_name.c_str());

However, having Customer be global is bad for a number of reasons. Instead, you should create a local instance in main and pass it to your functions. You will then be able to access it in the same manner.

Note: I used printf instead of cout. I would recommend getting familiar with it. You'll need to include stdio.h, I believe.

Also, you need to make sure you are passing your struct by reference. There are a number of reasons why you should do this, but you will need to in order to get the data back out.

void Save_Player_Name(Player_Data &Player) {<<stuff here>>}

You should also be declaring your functions before main:

struct Player_Data
{
   public: string Player_Name;// name of the player will be store here
};

void askUserForName(Player_Data &);
void writeNameToFile(Player_Data &);

void main()
{
   Player_Data player;
   askUserForName(player);

   return;
}

void askUserForName(Player_Data &player)
{
   <<do stuff>>
   writeNameToFile(player);

   return;
}

etc.

Unless you really need to use a struct, I would recommend going with classes. Structs make everything (variables and methods) public by default, whereas classes are private by default. In reality, structs and classes are identical--you can use them fairly interchangeably (don't shoot me!); in practice, structs are generally used when you need to aggregate some data (i.e., variables) without methods.

Your class might end up something like this (I haven't tested it, and I've been coding in Python lately, so please forgive any minor errors):

class PlayerData
{
   public:
      PlayerData()
      { 
      }
      ~PlayerData()
      {
      }

      void askUserForName()
      {
         <<code here>>
      }

      void writeNameToFile()
      {
          <<code here>>

          // also write to screen
          printf("[Write to console] name: %s\n", this->name_.c_str());
      }

   private:
      std::string name_;
};

void main()
{
   PlayerData player;
   player.askUserForName();
   player.writeNametoFile();

   return;
}

In reality, you'd want to use a header file and separate things out, but I'll leave that for another day.

1
  • thanks for the clarification an examples ...I was looking for the example similar to" If you are referring to how to access the data stored in the variable: Technically, you should be able to do the following from main:"
    – Cris
    Apr 1, 2014 at 22:10
0

You haven't called the method that saves the player after you call Save_Name_File() You need some logic fixes for your code

    #include <iostream>
    #include <fstream>
    using namespace std;
    struct Player_Data
    {
    public: string Player_Name;// name of the player will be store here
    }Customer[1];


    void Save_Player_Name(Player_Data Player)// will store the name of the player in a file
    {
          ofstream scores_data;
          scores_data.open ("scores.dat", std::ios_base::app);
          cout << Player.Player_Name << endl;
          scores_data<< Player.Player_Name << "\n";
          scores_data.close();

    }


    void Save_Name_File()// will capture the name of the player
    {

        int n;
        int i = 1;// number of players
        //cin.get();

        for (n=0; n<i; n++)// will the player
        {

         cout << "Player Amount " << n << " Out of : " << i;
        cout << "\n Please enter the name you wish to play \n\n Name: ";
        getline (cin,Customer[n].Player_Name);
        Save_Player_Name(Customer[n]);
        }



    }


    int main()
    {
        Save_Name_File();
        return 0;
    }
1
  • I actually had this code before but lets say for instance I have a menu in "int main" saying ... case 'a': Save_Name_File(); break; case 'b': //show player name, can i say ... " Save_Player_Name(Customer[1]); "
    – Cris
    Apr 1, 2014 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.