简单的游戏数据库问题



注意:我是C++新手,可能会做一些不好的做法,如果你看到这一点,请告诉我,以便我可以解决这个问题,请不要刻薄。我1-2个月前才开始编码。我还在学习。请对我可能不知道一切的事实持开放态度。

这是一个基于控制台文本的游戏。效果很好!虽然,我正在其中创建一个功能,允许用户在其上拖放任意数量的其他数据库以允许数据库传输。虽然这工作正常,但问题是我有一个小过程,它会尝试通过给它们放置一个数字来确保数据库中的信息都不相同,

示例,每个文件中将有 2 个配置文件 1。它们都被命名为 main。然后,用户将第二个数据库拖到游戏中,并将该数据库加载到原始数据库中。但是现在因为有 2 个相似的配置文件名称,它将无法区分哪个是哪个。因此,然后它通过一个小功能扫描数据库并在副本前面放置一个数字。从 5 开始,一路向上。虽然这似乎有效并且实际上并不难做到,但我遇到了一个问题,我不知道出了什么问题。我确实知道这与它如何扫描重复项有关。请帮忙。

我已经尝试了一整天,尝试不同的方法或重写代码。谷歌没有向我透露很多。

我在代码中使用以下库。(有些可能不会在示例中使用,但 tbh 我不记得哪个直接用于 THIS 函数)。

#include <iostream>
#include <iterator>
#include <cstring>
#include <cmath>
#include <cassert>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <utility>
#include <vector>
#include <array>
#include <functional>
#include <fstream>

这是扫描功能,以确保没有重复的配置文件。

让我解释一下会发生什么。 我制作了大量在数据库中使用的变量。正如您在数据库内部看到的,它有一定的顺序。

使用文件流访问数据库。(我有一个功能,它将结合用户拖动的所有数据库以及当前配置文件和数据,效果很好)。

数据库中的模式如下所示。

profile_name user_name 100 3 0 0 0 0 骑士 1 100 0 0

配置文件名称健康等

如果您查看变量,您将看到技术顺序。

void scanCopy()
{
std::string profile{ "John's_Profile" };
std::string name{ "John_Doe" };
int health{ 0 };
int damage{ 0 };
int gold{ 0 };
int exp{ 0 };
int level{ 0 };
int score{ 0 };
std::string CLASS{ "null" };
int dungeon{ 0 };
int maxHealth{ 0 };
int lives{ 0 };
int kills{ 0 };
std::ifstream in("data/database.txt");
std::vector <std::string> profiles;
int sizeOfVector{ 0 };
while (in >> profile >> name >> health >> damage >> gold >> exp >> level >> score >> CLASS >> dungeon >> maxHealth >> lives >> kills)
{
profiles.resize(sizeOfVector += 1);
profiles.at(sizeOfVector - 1) = { profile };
std::cout << profiles.at(sizeOfVector - 1) << "nn";
}
in.close();
for (int loop{ 0 }; loop < sizeOfVector; ++loop)
{
int compare{ loop };
for (int index{ loop }; index < sizeOfVector; ++index)
{
if (compare == index)//meaning they are like at profiles(1)and (1)
continue;
if (profiles.at(compare) == profiles.at(index))
{
std::ofstream out("data/~database.txt", std::ios::app);
in.open("data/database.txt");
int nameIndex{ 5 };
while (in >> profile >> name >> health >> damage >> gold >> exp >> level >> score >> CLASS >> dungeon >> maxHealth >> lives >> kills)
{
if (profile == profiles.at(index))
{
out << profile << nameIndex << " " << name << " " << health << " " << damage << " " << gold << " " << exp << " " << level << " " << score << " " << CLASS << " " << dungeon << " " << maxHealth << " " << lives << " " << kills << " " << std::endl; //Notice at the start profile is put into the database with an extra variable nameIndex to make its name now unique.
++nameIndex;
}
else
{
out << profile << " " << name << " " << health << " " << damage << " " << gold << " " << exp << " " << level << " " << score << " " << CLASS << " " << dungeon << " " << maxHealth << " " << lives << " " << kills << " " << std::endl;
}
}
}
}
}
in.close();
remove("data/database.txt");
in.open("data/~database.txt");
std::ofstream out("data/database.txt", std::ios::app);
//A buffer to copy everything inside a file to a string.
std::string upData((std::istreambuf_iterator<char>(in)),
(std::istreambuf_iterator<char>()));
/////
if (out)
out << upData; //putting everything in the tmp file to the file named database.txt
out.close();
in.close();
remove("data/~database.txt");
in.close();
out.close();
}

问题是它没有完成它的工作。它会把数字放在任何东西上。除此之外,它似乎也会溢出什么的。它的作用是在你已经把东西拖进去之后,它假装工作。然后,拖动它的任何更多输入都不会被扫描。问题是所有内容都是从用户从数据库中拖动的文件复制到 tmp 文件中的。然后删除数据库,并将临时文件重命名为 database.txt。问题是整个扫描功能似乎无法正常工作,我看不到其中的问题。有谁知道做这样的事情的好方法或问题是什么?谢谢!

我们真的不需要这个游戏的背景故事,用户可以做XYZ等等。请构建一个最小示例,如最小可重现示例。通常通过构建这些,您自己会发现问题。- 弗瑞什

谢谢弗瑞什。我发现了问题。我发送函数的次数太多,擦除了文件,或者它没有完全扫描它。很难解释我做过的真实事情,因为它很容易,但我不能很好地解释它。

总而言之。我检查并发现了该错误,我将其发送到我在那里发布太多次的此功能。或者时间太少。

最新更新