了解数据结构和类 C++.
Understanding Data Structures and Classes C++
所以我很难理解 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;
}
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 将文本文件解析为树状数据结构
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 带参数的数据结构的全局声明