gpt4 book ai didi

c++ - 将文件中的 0.0 读入 double Arr?

转载 作者:行者123 更新时间:2023-11-28 04:05:17 25 4
gpt4 key购买 nike

我正在尝试从如下所示的文件中读取一堆 0.0:

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

使用此代码进入双 arr:

double arr[9] = { 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 };
int tilechoice = 0, random;

ifstream inchoice("D:\\Code\\C++\\Tic Tac Toe AI\\Choices.txt");
inchoice >> fixed >> setprecision(1);
for (int i = 0; i < 11839; i++)
{
if (i == *b)
{
for (int j = 0; j < 9; j++)
{
inchoice >> arr[j];
}
break;
}
else
inchoice.ignore(36);

(*b是数组的第3行号)

但是当我测试其中包含 3 的行的 arr 的输出时,它向我显示:

0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0

3.0 哪里错了?

最佳答案

您的代码可以简化,从而根本不需要那个笨拙的 inchoice.ignore

ifstream inchoice("D:\\Code\\C++\\Tic Tac Toe AI\\Choices.txt");
std::string line;

for (int i = 0; std::getline(inchoice, line); ++i)
{
if (i == *b)
{
std::stringstream ss(line);
for (int j = 0; j < 9; j++)
{
inchoice >> arr[j];
}
}
}

关于c++ - 将文件中的 0.0 读入 double Arr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58831758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com