了解数据结构和类 C++.

Understanding Data Structures and Classes C++

本文关键字:C++ 数据结构 了解      更新时间:2023-10-16

所以我很难理解 Struct 是如何工作的,C++我已经开发了一个代码,我已经在其中玩了一段时间,但我似乎没有显示我正在寻找的结果。我没有收到任何编译器错误或错误,所以它似乎正在运行,这就是我所拥有的......

问:以后如何在"无效Save_Player_Name(Player_Data播放器)"中显示结果... ?

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 nn 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();
}

编辑:小修复.
编辑:添加了类注意事项。

问:如何在"void Save_Player_Name(Player_Data玩家)"以后... ?

如果您询问如何从文件中读取数据:

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>
   }
}

如果您指的是如何访问存储在变量中的数据:从技术上讲,您应该能够从main执行以下操作:

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

然而,由于多种原因,Customer全球化是不好的。相反,您应该在 main 中创建一个本地实例并将其传递给您的函数。然后,您将能够以相同的方式访问它。

注意:我用printf而不是cout。我建议熟悉它。我相信你需要包括stdio.h

此外,您需要确保通过引用传递结构。您应该这样做的原因有很多,但您需要这样做才能将数据取回。

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

您还应该在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.

除非你真的需要使用结构体,否则我建议你去上课。默认情况下,结构使所有内容(变量和方法)公开,而类默认为私有。实际上,结构和类是相同的 - 你可以相当互换地使用它们(不要向我开枪!在实践中,当您需要在没有方法的情况下聚合某些数据(即变量)时,通常使用结构。

你的类最终可能会像这样(我还没有测试过它,我最近一直在用 Python 编码,所以请原谅任何小错误):

class PlayerData
{
   public:
      PlayerData()
      { 
      }
      ~PlayerData()
      {
      }
      void askUserForName()
      {
         <<code here>>
      }
      void writeNameToFile()
      {
          <<code here>>
          // also write to screen
          printf("[Write to console] name: %sn", this->name_.c_str());
      }
   private:
      std::string name_;
};
void main()
{
   PlayerData player;
   player.askUserForName();
   player.writeNametoFile();
   return;
}

实际上,您希望使用头文件并将内容分开,但我会将其留到另一天。

调用 Save_Name_File() 后,您尚未调用保存播放器的方法您需要对代码进行一些逻辑修复

    #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 nn Name: ";
        getline (cin,Customer[n].Player_Name);
        Save_Player_Name(Customer[n]);
        }

    }

    int main()
    {
        Save_Name_File();
        return 0;
    }