所以我无法理解Struct在C ++中是如何工作的我已经开发了一段代码,我已经玩了一段时间,但我似乎没有显示我正在寻找的结果。我没有得到任何编译器错误或错误,所以它似乎正在运行,这就是我所拥有的......
问题:如何在以后的“void Save_Player_Name(Player_Data Player)”中显示结果...?
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();
}
答案 0 :(得分:1)
编辑:小修正。
编辑:增加了课堂考虑。
问题:如何在&#34; void中显示结果 Save_Player_Name(Player_Data Player)&#34;以后的......?
如果您询问如何从文件中读取数据:
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: %s\n", this->name_.c_str());
}
private:
std::string name_;
};
void main()
{
PlayerData player;
player.askUserForName();
player.writeNametoFile();
return;
}
实际上,您希望使用标题文件并将其分开,但我会将其留下另一天。
答案 1 :(得分:0)
在调用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 \n\n Name: ";
getline (cin,Customer[n].Player_Name);
Save_Player_Name(Customer[n]);
}
}
int main()
{
Save_Name_File();
return 0;
}