2013-10-24 52 views
1

我想從一個輸入文件讀取一組單詞到3個不同的字符串數組。文件中的單詞用'#'分隔。我的代碼出於某種原因運行了兩次,第一個數組是空白的,單詞是。請幫助,我的循環肯定是錯誤的,我必須忽略一些東西。請讓我知道我做錯了什麼。由於從輸入文件一次讀取一個字在c + +

Sample input file (input.txt) 

complicated 
insinuated 
complex 
juggernaut 
# 
blah 
... 
... 
# 
... 



#include <iostream> 
#include <fstream> 
#include <string> 
#include "conio.h" 

using namespace std; 

int main() { 

ifstream inFile("dictionary.txt"); 

// Check for error 
if (inFile.fail()){ 
    cout << "Error Opening File." << endl; 
    exit(1); 
} 

string hard[27], medium[29], easy[33]; 
string getHardWord, getMedWord, getEasyWord; 
int hardCount = 0, medCount = 0, easyCount = 0, delimitCount; // n is number of # 
    // delimiters and count is array position 

// Read the dictionary file until the end of file 
while (inFile){ 
    inFile >> getHardWord; 

    while ((getHardWord != "#") && (delimitCount = 0)){ 
     hard[hardCount] = getHardWord; 
     hardCount++; 
     inFile >> getHardWord; 
    } 

    delimitCount++; 
    cout << delimitCount << endl; 

    for (int iii = 0; iii < 27; iii++){ 
     cout << hard[iii] << endl; 
    } 

    cout << endl; 

    inFile >> getMedWord; 

    while ((getMedWord != "#") && (delimitCount = 1)){ 
     medium[medCount] = getMedWord; 
     medCount++; 
     inFile >> getMedWord; 
    } 

    delimitCount++; 
    cout << delimitCount << endl; 

    for (int jjj = 0; jjj < 27; jjj++){ 
     cout << medium[jjj] << endl; 
    } 

    cout << endl; 

    inFile >> getEasyWord; 

    while ((getEasyWord != "#") && (delimitCount = 2)){ 
     easy[easyCount] = getEasyWord; 
     easyCount++; 
     inFile >> getEasyWord; 
    } 

    delimitCount++; 
    cout << delimitCount << endl; 

    for (int kkk = 0; kkk < 27; kkk++){ 
     cout << easy[kkk] << endl; 
    } 

    inFile.close(); 
} 

_getch(); 

return 0; 
} 
+0

你是否在你的條件中對'=='錯誤'='? – DavidO

+0

是的,我是@DavidO。感謝您的每一個輸入,特別是user2599140。 –

回答

1

有一些小的失誤在這個代碼和示例文本文件:

1示例文件應該有一個#結尾,否則最後的循環將永遠運行。

2-

while ((getHardWord != "#") && (delimitCount = 0)) 

將總是爲假,因爲delimitCount將爲零。你應該在其聲明中初始化delimitCount爲零,在這種循環下降的分配,使之成爲:

while (getHardWord != "#") 

3-如果您要關閉該文件的最後,你的病情的同時應檢查該文件是開放的,所以不是:

while (inFile) 

使用

while (inFile.is_open()) 

我測試你的代碼,這些變化和它工作得很好:

// Test.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include "conio.h" 

using namespace std; 

int main() { 

ifstream inFile("dictionary.txt"); 

// Check for error 
if (inFile.fail()){ 
    cout << "Error Opening File." << endl; 
    exit(1); 
} 

string hard[27], medium[29], easy[33]; 
string getHardWord, getMedWord, getEasyWord; 
int hardCount = 0, medCount = 0, easyCount = 0, delimitCount=0; // n is number of # 
    // delimiters and count is array position 

// Read the dictionary file until the end of file 
while (inFile.is_open()){ 
    inFile >> getHardWord; 

    while ((getHardWord != "#")){ 
     hard[hardCount] = getHardWord; 
     hardCount++; 
     inFile >> getHardWord; 
    } 

    cout<<hard<<endl; 

    cout<<endl; 


    delimitCount++; 
    cout << delimitCount << endl; 

    for (int iii = 0; iii < 27; iii++){ 
     cout << hard[iii] << endl; 
    } 

    cout << endl; 

    inFile >> getMedWord; 

    while ((getMedWord != "#") && (delimitCount = 1)){ 
     medium[medCount] = getMedWord; 
     medCount++; 
     inFile >> getMedWord; 
    } 

    delimitCount++; 
    cout << delimitCount << endl; 

    for (int jjj = 0; jjj < 27; jjj++){ 
     cout << medium[jjj] << endl; 
    } 

    cout << endl; 

    inFile >> getEasyWord; 

    while ((getEasyWord != "#") && (delimitCount = 2)){ 
     easy[easyCount] = getEasyWord; 
     easyCount++; 
     inFile >> getEasyWord; 
    } 

    delimitCount++; 
    cout << delimitCount << endl; 

    for (int kkk = 0; kkk < 27; kkk++){ 
     cout << easy[kkk] << endl; 
    } 

    inFile.close(); 

} 

_getch(); 

return 0; 
} 
0

是否有你沒有使用getline的原因?如果我正確理解你的代碼,只需在該函數中使用#作爲分隔符就可以簡化它。

for (int i = 0; i < 27; i++) //It looks like there are 27 words per type, if that's wrong change this 
{ 
    getline(inFile, getHardWord, '#'); 
    hard[i] = getHardWord; 
} 
//And so on for the other difficulties.