大きなテキスト ファイルから単語を推測するプログラムを作成しています。1 つのステップは、ストリングの長さを判別するためにユーザー入力を取得することです。
編集:完全なコードを追加し、いくつかの変更を加えました
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int i(0),n(0),counter(0),limit(0);
char words[60000][30];
int initarray() {
int length(0);
string line;
char temp;
ifstream wordlist ("text.txt");
if (wordlist.is_open())
{
while (wordlist.good())
{
getline (wordlist,line);
length=line.length();
for (n=0;n!=length;n++)
{
temp=line.at(n);
words[i][n]=temp;
}
i++;
counter++;
}
}
else
{
cout<<"file not opened";
}
wordlist.close();
return 0;
}
int selectlength()
{
int length;
bool shorter(false),longer(false);
cout <<"length of word"<<endl;
cin >> length
limit=counter;
counter=0;
for (i=0;i<limit;i++){
for (n=0;n!=length;n++){
if (words[i][n]=='\0')
{
shorter=true;
break;
}
}
if (words[i][length+1] != '\0')
{
longer=true;
}
if (longer==true || shorter==true)
{
i--;
}
}
return 0;
}
int printresults(){
for (i=0;i!=counter;i++){
for (n=0;n<=20;n++){
cout << words[i][n];
}
cout <<endl;
}
return 0;
}
int main() {
initarray();
selectlength();
printresults();
return 0;
}
しかし、私の問題は、正常にコンパイルされたプログラムが「cin」部分に到達してユーザー入力の長さを読み取るたびに発生することです。任意の数字を入力してエンターを押しても何も起こりません。プログラムはまだ実行されており、無期限に入力を受け取り続けています。何か助けはありますか?別の関数ではありますが、プログラムの早い段階で ifstream を使用することと関係がありますか?