1

入力ファイルから一連の単語を 3 つの異なる文字列配列に読み込もうとしています。ファイル内の単語は「#」で区切られています。何らかの理由でコードが 2 回実行され、最初の配列は空白で、単語は . 助けてください、私のループは間違いなく間違っていて、何かを見落としているに違いありません。私が間違っていることを教えてください。ありがとう

 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;
}
4

2 に答える 2

1

このコードとサンプル テキスト ファイルには、いくつかの小さな間違いがあります。

1- サンプル ファイルの末尾に # を付ける必要があります。そうしないと、最後のループが無限に実行されます。

2-

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

delimitCount がゼロになるため、常に false と評価されます。宣言で delimitCount をゼロに初期化し、このループで代入をドロップして、次のようにする必要があります。

while (getHardWord != "#")

3-最後にファイルを閉じる場合、 while 条件は、ファイルが開いていることを確認する必要があるため、次の代わりに:

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;
}
于 2013-10-24T05:33:48.603 に答える
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.
于 2013-10-24T05:37:13.320 に答える