了解数据结构和类C++; P>所以我很难理解C++中的结构是如何工作的,我开发了一个代码,我已经玩了一段时间,但是我似乎并没有显示出我正在寻找的结果。我没有得到任何编译器错误或错误,所以它似乎正在运行,这就是我所拥有的
问题:以后如何在“无效保存球员姓名(球员数据球员)”中显示结果了解数据结构和类C++; P>所以我很难理解C++中的结构是如何工作的,我开发了一个代码,我已经玩了一段时间,但是我似乎并没有显示出我正在寻找的结果。我没有得到任何编译器错误或错误,所以它似乎正在运行,这就是我所拥有的,c++,data-structures,void,C++,Data Structures,Void,问题:以后如何在“无效保存球员姓名(球员数据球员)”中显示结果 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;
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();
}
struct Player\u数据
{
public:string Player_Name;//将在此处存储播放器的名称
}客户[1];
int main()
{
保存_Name_File();
}
void Save_Name_File()//将捕获播放器的名称
{
int n;
int i=1;//玩家数量
//cin.get();
对于(n=0;n在调用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;
}
#包括
#包括
使用名称空间std;
struct Player_数据
{
public:string Player_Name;//将在此处存储播放器的名称
}客户[1];
void Save\u Player\u Name(Player\u Data Player)//将播放器的名称存储在文件中
{
数据流分数;
scores\u data.open(“scores.dat”,std::ios\u base::app);
无法编辑:次要修复。
编辑:添加了类考虑因素
问题:如何在“void”中显示结果
以后保存\u播放器\u名称(播放器\u数据播放器)“
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();
}
如果您询问如何从文件中读取数据:
const bool readFile()
{
ifip流;
ip.open(“scores.dat”,ifstream::in);
如果(!ip)
{
printf(“无法打开文件”);
返回false;
}
//循环文件中的每一行
字符串bffr;
while(getline(ip,bffr))
{
}
}
如果您指的是如何访问存储在变量中的数据:
从技术上讲,您应该能够从main
执行以下操作:
Save_NameFile();
printf("Player name: %s", Customer[n].Player_name.c_str());
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.
但是,让Customer
成为全局的是不好的,原因有很多。相反,您应该在main中创建一个本地实例,并将其传递给您的函数。然后您将能够以相同的方式访问它
注意:我使用了printf
而不是cout
。我建议您熟悉它。我相信您需要包括stdio.h
此外,您还需要确保通过引用传递结构。您应该这样做的原因有很多,但您需要这样做才能恢复数据
void Save_Player_Name(Player_Data &Player) {<<stuff here>>}
除非您真的需要使用结构,否则我建议您使用类。默认情况下,结构使所有内容(变量和方法)都是公共的,而默认情况下,类是私有的。事实上,结构和类是相同的——您可以相当互换地使用它们(不要批评我!);在实践中,当您需要在没有方法的情况下聚合某些数据(即变量)时,通常使用结构
您的类可能会以类似的方式结束(我还没有测试它,最近我一直在用Python编写代码,所以请原谅任何小错误):
class PlayerData
{
公众:
PlayerData()
{
}
~PlayerData()
{
}
void askUserForName()
{
}
void writeNameToFile()
{
//也写到屏幕上
printf(“[写入控制台]名称:%s\n”,this->name_u.c_str());
}
私人:
std::字符串名称;
};
void main()
{
玩家数据播放器;
player.askUserForName();
player.writeNametoFile();
返回;
}
实际上,您可能希望使用一个头文件并将其分离出来,但我将把它留到另一天。我以前确实有过这段代码,但假设我在“int main”中有一个菜单,上面写着……case‘a’:Save_Name_file();break;case‘b’://show player Name,我可以说……“Save_player_Name(Customer[1])”感谢您对示例的澄清…我正在寻找类似于“如果您指的是如何访问存储在变量中的数据:从技术上讲,您应该能够从main执行以下操作:”