2014-12-27 60 views
-2
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://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – 2014-12-27 16:52:44

回答

0

getline「滿」代碼失敗在最後輸入線和設置故障位,而不是ficheof。 結論:從不使用while (!file.eof()){...}而沒有在循環內部進行額外的測試,否則會遇到麻煩。

+0

有任何建議爲額外的測試?我在C++中缺乏處理文件的經驗,謝謝。 – epiClowN 2014-12-27 17:21:30

+0

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

0

我不是一個很聰明的人,我必須承認,發現了問題,那就是:

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(...); 

固定的,非常感謝你,雖然。