2016-08-12 150 views
1

我正在使用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正在访问这些文本文件。

这里是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; 
} 

回答

0

您的问题是scramlen未初始化。所以bool match[scramlen];string used[scramlen];将阻止你编译。

考虑使用来自库#include <vector>std::vector而不是array。您将能够像数组一样访问元素,但要动态调整大小。和用法是例行类似:

int scramlen = scram.length(); 
std::vector<bool> match(scramlen); 
std::vector<int> used(scramlen); 

// .. 

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

编辑:

从阅读的评论,它看起来像我的一件事糊涂了。你不能用变量初始化一个数组,这是为什么:Array[n] vs Array[10] - Initializing array with variable vs real number。你将需要一个不变的整数。然而,使用std::vector是解决方案。

+0

编译错误的原因与变量未被初始化无关。 – PaulMcKenzie

+0

我读了你的答案,我什么都没学到。你能告诉我那是什么原因吗? –

+0

我的回答中有什么不明白的地方?这很清楚--OP正试图用变量声明一个数组作为项目的数量。这不是合法的C++。你声称原因是'scramlen'没有被初始化 - 这显然是错误的。不管它是否被初始化,都无关紧要,你不能使用变量声明数组作为项目的数量。 – PaulMcKenzie

1

这不是合法的C++:

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

阵列在C++中必须使用编译时间常数来表示的条目的数量,而不是一个变量。该语法可以与支持可变长度数组(VLA)的编译器一起使用,但这是编译器扩展,因此是非标准的。

这种扩展是由编译器,如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; 
+0

非常感谢。我不知道标准库中的vector类,现在我明白了我在编译时遇到的错误。我相应地调整了我的代码,现在编译时没有问题......但是我遇到了另一个错误! :(我会尝试自己解决,再次感谢!!!) –

相关问题