2015-01-16 25 views
-3

我想寫一個程序,將輸入文件與充滿大量單詞的字典文件進行比較。比較單詞後,我想輸出拼寫錯誤的單詞。這裏是我的代碼:C++字典與多個字符串和文件比較

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <vector> 
using namespace std; 

void trim(string s) 
{ 
    size_t p = s.find_first_not_of(" \t"); 
    s.erase(0, p); 
    p = s.find_last_not_of(" \t"); 
    if (string::npos != p) 
     s.erase(p+1); 
} 

int main() 
{ 
    ifstream input; 
    ifstream words; 

    input.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/input.txt"); 
    if(input.fail()) 
    { 
     cout<<"Input file opening failed"; 
     exit(1); 
    } 
    words.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/words.txt"); 
    if(words.fail()) 
    { 
     cout<<"Words file opening failed"; 
    } 

    vector <string> wordCheck; 
    vector <string> misspelledWord; 
    string temp = ""; 

    while(!input.eof()) 
    { 
     input>>temp; 
     wordCheck.push_back(temp); 

    } 

    ofstream output; 
    output.open("/Users/jordan/Desktop/CS60/Word_dictionary_check/output.txt"); 
    if(output.fail()) 
    { 
     cout<<"Output file opening failed"; 
     exit(1); 
    } 

    for(int j = 0; j < wordCheck.size(); j++) 
    { 
     bool dontprint = false; 
     while(!words.eof()) 
     { 
      words>>temp; 
      if(temp == wordCheck[j]) 
      { 
       dontprint = true; 
      } 

     } 
     if(dontprint == false) 
     { 
      misspelledWord.push_back(wordCheck[j]); 
     } 
    } 

    for(int i = 0; i < misspelledWord.size() ; i++) 
    { 

     output<<misspelledWord[i]<<endl; 

    } 

    return 0; 


} 

我相信有空白或與字符串比較是一個問題。謝謝你的協助!

+1

如果你打算做家庭作業,你必須給出一個不太模糊的[問題陳述](http://stackoverflow.com/help/mcve)。 – user657267

回答

0

我可以看到幾個明顯的問題。我已添加評論。這應該可以解決你的問題,但我不會爲你寫代碼。

for(int j = 0; j < wordCheck.size(); j++) 
{ 
    bool dontprint = false; 

    //Make your words file pointer to point to start of file. USe seek function 
    while(!words.eof()) 
    { 
     words>>temp; 
     if(temp == wordCheck[j]) 
     { 
      dontprint = true; 
      //You can break here. As once word is found, you don't need to check the word file further 
     } 

    } 
    if(dontprint == false) 
    { 
     misspelledWord.push_back(wordCheck[j]); 
    } 
}