c++循环随机剪切
c++ Cycle cuts out randomly?
首先我有这个类:
class Recept
{
private:
int serves;
string* ingredient_name;
int* ingredient_number;
float difficulty;
public:
Recept(int a=0, string* b = NULL, float c = 0.0, int* d = NULL)
{
serves = a;
ingredient_name = b;
difficulty = c;
ingredient_number = d;
}
~Recept()
{
delete ingredient_name;
delete ingredient_number;
}
};
存储所有可用配方的对象:
Recept* AvailableRecipes;
和这个函数初始化这个对象。main()所做的唯一一件事就是调用这个函数。
void OpenRecipes()
{
SetCurrentDirectory("Recipes");
system("dir /b > a.txt");
ifstream filelist;
filelist.open("a.txt");
stringstream newstrstr;
newstrstr << filelist.rdbuf();
string seged = newstrstr.str();
filelist.clear();
filelist.seekg(0, ios::beg);
newstrstr.str(std::string());
AvailableRecipes = new Recept[count_words(seged)-1];
string filename;
int counter = 0;
cout << "Total number of iterations needed: " << count_words(seged) << endl;
for(int i = 0; i < count_words(seged) ; i++)
{
cout << "i: " << i << endl;
filelist >> filename;
if(filename != "a.txt")
{
stringstream newstrstr;
ifstream input;
input.open(filename.c_str());
newstrstr << input.rdbuf();
string seged2 = newstrstr.str();
int ingredient_num[(count_words(seged2) - 2) / 2];
string ingredient_name[(count_words(seged2) - 2) / 2];
float difficulty;
int serving;
input.clear();
input.seekg(0, ios::beg);
input >> serving >> difficulty;
int IntContain;
string StringContain;
for (int j = 0; j < sizeof(ingredient_num)/sizeof(ingredient_num[0]); j++)
{
input >> IntContain >> StringContain;
ingredient_num[j] = IntContain;
ingredient_name[j] = StringContain;
}
Recept a = Recept(serving, ingredient_name, difficulty, ingredient_num);
AvailableRecipes[counter] = a;
counter++;
newstrstr.str(std::string());
input.close();
cout << "No error so far" << endl;
}
}
}
基本上,此函数应该:-从子文件夹/配方读取文件名
-将文件名存储在同一文件夹中的".txt"中。
-打开文件1乘1,并根据其中的文本创建Recipe对象。
-将Recipe对象添加到AvailableRecipes对象数组中。
问题是,由于某种原因,这个周期似乎是随机中断的。我想知道为什么,以及如何修复它:
示例输出:
Total number of iterations needed: 4
i: 0
No error so far
i: 1
i: 2
Process returned -1073741819 (0xC0000005) execution time : 1.312 s
Press any key to continue.
//在本例中,迭代0使用的是一个有效的文件(!="a.txt"),迭代1处理的是".txt",迭代2是另一个有效文件
我是个菜鸟,非常好,所以请友善一点:/使用CodeBlocks,在win64 上使用minGW
这根本不是随机的。根据您发布的代码了解:1) 有4个字需要存储在数组中。
2) 您创建了一个大小为[word_count]-1=3的数组(即索引i范围为[0..2])
3) 您的for循环有4次迭代(与[word_count]相同)(即遍历[0..3]范围内的索引)
4) 当您尝试访问索引为3
的数组元素时,会出现访问冲突错误,因为它不存在(请记住:您的数组的最大索引为2
)。这就是异常代码0xC0000005
的含义——访问违规。
这两行已经存在两个问题
Recept a = Recept(serving, ingredient_name, difficulty, ingredient_num);
AvailableRecipes[counter] = a;
首先创建a
,它存储指向局部变量ingredient_name
和ingredient_num
的指针。一旦您离开声明这些指针的if语句,这些指针就会悬空。
然后在下一行中,在AvailableRecipes
数组中创建a
的副本。现在,每个指针都有两个副本。
最终,所有这些都将在Recept
的析构函数中结束,试图删除指针。它们不仅悬挂着,而且每个都有几个副本。双重错误!
- 循环中的随机函数
- C++在乘法图中放置随机值(嵌套循环)
- 如何在循环中随机化不同的值?c ++
- 骰子滚轮随机循环每次运行时都保持相同的值
- C 编程:在循环时运行 2 并从 loop1 获取数据的随机结果
- 随机函数在循环中调用时返回相同的数字!C++
- C++ 通过 range for 循环将整数输入到矢量中;随机 0
- 随机返回相同的循环值
- 用于随机访问和元素循环的最佳数据结构(C++)
- 随机密码生成器的C++循环错误"以非零状态退出"
- 线程在循环的中间随机停止
- 执行随机for循环并每次保存随机值
- for循环导致随机分段错误C++
- 生成具有负边权重且无负循环的随机图
- C++随机化和循环困难
- 随机化不会在 while 循环中发生。如何解决这个问题?C++
- 每次循环进行临时均匀随机分布的效率如何
- C++循环,直到随机生成等于某个数字
- 随机循环访问映射会导致段错误
- 如何让敌人在一定距离内随机循环并跟随玩家?c++主机游戏