我正在使用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