2016-08-12 147 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類,現在我明白了我在編譯時遇到的錯誤。我相應地調整了我的代碼,現在編譯時沒有問題......但是我遇到了另一個錯誤! :(我會嘗試自己解決,再次感謝!!!) –

相關問題