我正在使用Microsoft Visual C++ 2010 Express。运行我的代码调试结果在以下错误:C++ - 从文本文件中获取值以进行比较
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”文件。然后将它与用于匹配字符的“wordlist.txt”内容进行比较。
比较的字符串和成功匹配的字符串应该通过控制台窗口显示出来并导出到“output.txt”。
我已将“wordlist”和“input”文本文件放在工作目录中(即与.vC++ proj文件相同),但从失败的调试中判断,我不认为ifstream正在访问这些文本文件。
这里是代码:
#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;
}
编译错误的原因与变量未被初始化无关。 – PaulMcKenzie
我读了你的答案,我什么都没学到。你能告诉我那是什么原因吗? –
我的回答中有什么不明白的地方?这很清楚--OP正试图用变量声明一个数组作为项目的数量。这不是合法的C++。你声称原因是'scramlen'没有被初始化 - 这显然是错误的。不管它是否被初始化,都无关紧要,你不能使用变量声明数组作为项目的数量。 – PaulMcKenzie