2012-03-30 85 views
2

好了,所以我確定即時通訊做一些愚蠢的事:d簡單的C++函數

我有一個函數:

int wordFunc(string a){ 
    std::ifstream inp; 
    inp.open(a, std::ios::in); 
    if (inp.is_open()){ 
     std::string word; 
     unsigned long wordCount = 0; 
     while(!inp.eof()){ 
      inp >> word; 
      while(word.length() > 0){ 
       wordCount++; 
      } 
      inp.close(); 
     } 
     return wordCount; 
    } 
} 

字符串是用戶輸入file.txt的 - 其設置爲C:\轉儲\ user.txt現在

當我調用代碼:

int main(){ 
    string file; 
    int words = 0; 
    file = "C:\\Dump\\user.txt"; 

    int a = wordFunc(file, words); 
    cout << "Words: " << a << endl; 

    return 0; 
} 

控制檯剛剛停止 - 我havnt編碼任何我ñC++在很多年,所以即時通訊生鏽 - 任何幫助?

編輯 有了某種幫助高度重視和我結束了會是這樣

unsigned long wordFunc(const std::string& a){ 
    std::ifstream inp(a); 
    system("cls"); 
    unsigned long wordCount = 0; 
    std::string word; 
    while(inp >> word) 
    { 
     wordCount++; 
    } 
    return wordCount; 
} 

對於功能 - 應該已經發布了更新

+0

你確定你有正確的接口?在主程序中,你用兩個參數(文件和單詞)調用wordFunc,並且該函數只有一個參數(a)。 – Glenn 2012-03-30 04:09:25

+0

是的 - 我也抓到了 - 這絕對是while循環 – 2012-03-30 04:27:31

+0

這是功課嗎? – Johnsyweb 2012-03-30 10:02:49

回答

4

你的問題是這樣的:

 while(word.length() > 0){ 
      wordCount++; 
     } 

這將永遠循環。你可能的意思是

 if(word.length() > 0){ 
      wordCount++; 
     } 
+0

HA - 我知道這是redic的 - 現在我只計算2個單詞,但我可能會想出來的結果 – 2012-03-30 04:24:34

+0

@Hazy,這是因爲你正在關閉讀取循環內的文件,這意味着只有一行被讀取 - 結合對EOF標誌設置的誤解,就是爲什麼你只能得到'2'作爲返回值。一旦你解決了這個問題,你還可以看一些其他的東西(看我的答案)。 – paxdiablo 2012-03-30 04:35:52

1

你有很多問題。


至於另一個海報評論說,該行:

while (word.length() > 0) 

將永遠循環下去,你需要將其更改爲:

if (word.length() > 0) 

您混用整數和無符號長整型不當。 wordCounta變量以及來自wordFunc()的返回值應該是相同的。


你有inp.close()讀取循環,而不是它屬於外面的位置。這意味着它將在處理第一行後關閉文件。


你也有if聲明意味着你有一個語法錯誤,在一個執行路徑(就是文件無法打開)不返回任何內部return語句。

您需要更換return和後面的右大括號。

這也意味着wordCount變量也必須聲明在if語句之外(在函數的頂層)。


我覺得fstream::open()需要char*而不是字符串,所以你應該重新編寫它:

inp.open (a.c_str(), std::ios::in); 

您的來電wordFunc(),有兩個參數,不匹配的原型,只有一個。


有一些外部變量,如wordsmain()


最後字被計算兩次因爲一旦你嘗試讀取超越文件末尾的EOF標誌只設置。您可以修復與簡單的修改到if聲明遞增wordCount

if ((!inp.eof()) && (word.length() > 0)) { 

與所有這些進行更改,則結束:

#include <iostream> 
#include <fstream> 

unsigned long wordFunc (std::string str) { 
    unsigned long wordCount = 0; 
    std::ifstream inp; 

    inp.open(str.c_str(), std::ios::in); 
    if (inp.is_open()) { 
     std::string word; 
     while (!inp.eof()) { 
      inp >> word; 
      if ((!inp.eof()) && (word.length() > 0)) { 
       wordCount++; 
      } 
     } 
     inp.close(); 
    } 
    return wordCount; 
} 

int main() { 
    std::string file; 
    file = "user.txt"; 
    unsigned long a = wordFunc(file); 
    std::cout << "Words: " << a << std::endl; 
    return 0; 
} 
+0

不錯。但是我可能會轉回到返回一個int,並且在打開一個文件失敗時返回'-1'。然後我倒置'if',所以你最終得到'if(!inp.is_open()){return -1}',其餘的邏輯縮減一個縮進級別。 – 2012-03-30 04:39:39

1

C++已經擁有的功能在標準庫做大部分的事情,所以你可以做類似的事情:

#include <fstream> 
#include <iostream> 
#include <algorithm> 
#include <iterator> 
#include <string> 

int main() { 
    std::ifstream in("user.txt"); 

    std::cout << "Words: " 
       << std::distance(std::istream_iterator<std::string>(in), 
           std::istream_iterator<std::string>()); 
    return 0; 
} 

得到三個可執行語句需要五個頭文件的愛代碼!

如果你堅持做自己的計數,我會做一些事情,如:

std::ifstream in("user.txt"); 

std::string word; 

while (in >> word) 
    ++wordCount; 

std::cout << "Words: " << wordCount; 
+0

太好了 - 謝謝! – 2012-03-30 10:43:38