gpt4 book ai didi

C++ - 从文本文件中获取值到数组中进行比较

转载 作者:行者123 更新时间:2023-11-30 05:24:00 24 4
gpt4 key购买 nike

我使用的是 Microsoft Visual C++ 2010 Express。运行我的代码调试会导致以下错误:

    1>------ Build started: Project: Word Unscrambler, Configuration: Debug Win32 ------
1> word unscrambler.cpp
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(22): error C2057: expected constant expression
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(22): error C2466: cannot allocate an array of constant size 0
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(22): error C2133: 'match' : unknown size
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(23): error C2057: expected constant expression
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(23): error C2466: cannot allocate an array of constant size 0
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(23): error C2133: 'used' : unknown size
1>c:\users\c m j richards\documents\visual studio 2010\projects\word unscrambler\word unscrambler\word unscrambler.cpp(59): warning C4154: deletion of an array expression; conversion to pointer supplied
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我实际上是在尝试通过使用大小等于从“input.txt”文件传递到函数中的单词的字符串长度的 bool 数组来创建单词解码器。然后将其与匹配字符的“wordlist.txt”内容进行比较。

比较后的字符串,以及那些成功匹配的字符串应该通过控制台窗口显示出来,并导出到一个“output.txt”。

我已经将“wordlist”和“input”文本文件放在工作目录中(即与 .vc++proj 文件相同)但是从失败的调试来看,我不认为 ifstream 正在访问这些文本文件.

这是IDE的截图: enter image description here

代码如下:

#include<string>
#include<cstdio>
#include<iostream>
#include<fstream>

using namespace std;

string unscramble(string scram)
{
int scramlen = scram.length();
int i = 0;

string word;
ifstream file("wordlist.txt");
if (file.is_open())
{
while (file.good())
{
getline(file,word);
if (scramlen == word.length())
{
bool match[scramlen];
string used[scramlen];
int matchcount = 0;

for (int x = 0; x < scramlen; x++)
{
string lttrscram = scram.substr(x,1);

for (int y = 0; y < scramlen; y++)
{
string lttrunscram = word.substr(y,1);

if (lttrscram == lttrunscram)
{
if (used[y] == lttrscram) match[matchcount] = false;

else
{
used[y] = lttrscram;
match[matchcount] = true;
matchcount++;
break;
}
}
}
}

i = 0;
for (int j = 0; j < scramlen; j++)
{
if (match[j] == true) i++;
}
if (i == scramlen)
{
cout <<"Match found: " << word << endl;
return word;
}
delete [] match;
}
}
file.close();
}
}

int main()
{
string inputkey[10];
string outputkey[10];
int wordnum = 0;

int count = 0;
string wordtemp;
ifstream file("input.txt");
if (file.is_open())
{
while (file.good());
{
getline (file,wordtemp);
inputkey[count] = wordtemp;
count++;
}
file.close();
}

for (int i = 0; i < 10; i++)
{
wordnum++;
cout <<"#" << wordnum << " Comparing: " << inputkey[i] << endl;
outputkey[i] = unscramble(inputkey[i]);
}

ofstream output;
output.open("output.txt");

for (int j = 0; j < 10; j++)
{
if (j == 9) output << outputkey[j];
else output << outputkey[j] << ", ";
}
output.close();

system("pause");
return 0;
}

最佳答案

这不是合法的 C++:

int scramlen = scram.length();
//...
bool match[scramlen];
string used[scramlen];

C++ 中的数组必须使用编译时常量来表示条目数,而不是变量。此语法可能适用于支持可变长度数组 (VLA's) 的编译器,但这是编译器扩展,因此是非标准的。

此扩展被g++等编译器支持,但并且从来没有被Visual C++系列编译器支持(也没有必要支持它,同样,以这种方式声明数组是不合法的 C++。

无论如何,我建议不要使用 VLA,即使编译器确实支持它们。相反,如果您想要一个动态数组,请使用 std::vector。它是标准的 C++(因此适用于所有编译器),并为您提供检查数组边界(使用 vector::at())等优势,这是 VLA 无法做到的。

#include <vector>
//...
std::vector<bool> match(scramlen); // See item below
std::vector<string> used(scramlen);

此外,您在对非指针类型发出 delete [] 调用时犯了一个错误。删除这一行:

 delete [] match;

关于C++ - 从文本文件中获取值到数组中进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38910753/

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