我一直在這裏待了幾個小時,而且我很難讀入我的文本文件,計算每個單詞有多少個字母,每個字的數量字母。將word.length()存儲並輸出到數組中
我已經提出了這個問題,迄今:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
const int array_size = 29;
int main() {
ifstream inputfile;
string word, word2;
int wordlength[array_size];
int length = 0;
cout << left << setw(10) << "Length: ";
cout << left << setw(10) << "# of words: " << endl;
inputfile.open("C:/EnglishWords.txt");
while (inputfile) {
inputfile >> word;
int len = word.length();
wordlength[len]++; //initialized array for '29'
for (int i = 1; i < 29; i++) {
cout << left << setw(10) << wordlength[i];
cout << left << setw(10) << i;
}
}
getchar();
getchar();
return 0;
}
我基本上得到的-8293729變化(我假定這是垃圾存儲器),選擇那些我要打印的每個實際值。我真的可以使用這個stackoverflow的力量,因爲我很難過:/。
編輯:我正在閱讀的文件是由「/」分隔的所有英文單詞的列表;
除了嵌套'for'循環(哦,這是一個缺少'}'?感謝[MCVE]),並檢查流錯誤** **之前提取,你沒有向我們顯示錯誤。 'wordlength'可能有未初始化的元素。它應該是'std :: map' ... – LogicStuff
嘿,謝謝你的迴應。我在while循環結尾添加了'}'。所以這不是問題。我有點困惑,因爲我會告訴你錯誤,通過提供int main()部分,所以你可以編譯它?另外,我是編碼方面的新手,對於如何實現地圖我有點困惑,我查了一下,但仍然有點失落。再次感謝您的迴應! – Complexicator
這個'while'循環可能應該是'while(inputFile >> word){...}' – KABoissonneault