class Interacao{
vector<string> v_line;
...
public:
void comando_load(istringstream & iss);
};
void Interacao::comando_load(istringstream & iss){
v_line.empty();
string line;
string fname = "ini.txt";
int it = 0;
ifstream file(fname);
while (!file.eof()){
it++;
getline(file, line);
cout << line << endl; //this works like it should if the line below is commented
v_line.push_back(line); //this keeps adding line to the vector infinitely
}
file.close();
}
我試圖做的是從一個文本文件(而不是在控制檯輸入它們)讀取文件,將它們放置在一個字符串矢量然後用一些修改(iss.str(line),getline(cin,line)disabled-> flag)使用相同的控制檯插入命令代碼讀取它們,但它不會停止向該向量添加行...- >添加到載體...無限循環
對不起,很不清楚,太混淆了,無法清楚地識別/解釋問題。
預先感謝您, -eC。
編輯:下面
class Interacao{
Consola c;
vector<string> v_linha;
public:
};
void Interacao::comando_load(istringstream & iss){
v_linha.empty();
string linha;
string nome_fich = "ini.txt";
ifstream fich(nome_fich);
while (!fich.eof()){
getline(fich, linha);
v_linha.push_back(linha); //adds command from file to vector
}
fich.close();
}
void Interacao::Le_Comando(){
string linha;
Populacao* pop_jogador = nullptr;
bool flg_pop_jgdr = 0;
unsigned int cont = 0;
unsigned int it = 0;
int iComando = 0;
int iCalls = 0;
do{
c.gotoxy(3, 56); //error: this goes to a specific place on the console, it's on infinite cycle thus not allowing any action
if (linha.length() != 0)
c.clrcmd(linha.length());
while (it < v_linha.size()){
linha = v_linha[it];
it++;
break;
}
cont = v_linha.size(); //if the vector is not empty, disables cin, reads from vector instead instead cont = EOF
if (cont == 0){
getline(cin, linha);
}
if (cont > 0){ //has commands to read in vector, read 1 command, decreases cont
cont--;
}
istringstream iss;
iss.str(linha); //command
iComando = escolheComando(iss); //converts string to int for the switch
switch (iComando){
case 0: //sair (exit)
cout << "Terminou o jogo. A desligar...";
exit(1); break;
case 1: //load
comando_load(iss);
break;
}
} while (iComando != 0);
}
你沒有正確使用'getline'。 – 2014-12-27 16:15:35
@epiClowN什麼是在無限循環內輸出? – 2014-12-27 16:43:28
另請注意,while(!file.eof()){':http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – 2014-12-27 16:52:44