- >添加到载体...无限循环

问题描述:

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); 
} 
+2

你没有正确使用'getline'。 – 2014-12-27 16:15:35

+0

@epiClowN什么是在无限循环内输出? – 2014-12-27 16:43:28

+0

另请注意,while(!file.eof()){':http://*.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – 2014-12-27 16:52:44

getline“满”代码失败在最后输入线和设置故障位,而不是ficheof。 结论:从不使用while (!file.eof()){...}而没有在循环内部进行额外的测试,否则会遇到麻烦。

+0

有任何建议为额外的测试?我在C++中缺乏处理文件的经验,谢谢。 – epiClowN 2014-12-27 17:21:30

+0

http://*.com/questions/7868936/read-file-line-by-line – zkoza 2014-12-27 17:44:49

我不是一个很聪明的人,我必须承认,发现了问题,那就是:

do{ 
cont = v_linha.size(); //<<<<<<<< can't be inside the loop.... 
    if (cont == 0){ 
     getline(cin, linha); 
    } 

    if (cont > 0){ //has commands to read in vector, read 1 command, decreases cont 
     cont--; //<<<<<<<<<< ....for this to work properly... 
    } 
}while(...); 

固定的,非常感谢你,虽然。