2012-09-26 41 views
0

我試圖從標有{1 ... 9}或X的文件讀取輸入。我需要分開這些值並將它們正確存儲在向量中。我使用的是「sstringstream」幫我這樣做:發生在if語句,當在「輸入」的整數它不執行if語句Isstringstream不適用於整數

void myclass::Initialize(ifstream &file_name) 
{ 
    string input; 
    int value; 
    //Initialize the values in the matrix from the values given 
    //in the input file, replace x with a 0 
    for(int i = 0; i < 9; i++) 
    { 
     for(int j = 0; j < 9; j++) 
     { 
      //Read the input from the file and determine 
      //if the entry is an "int" or "x" 
      file_name >> input; 
      cout << input << endl; 
      istringstream(input); 
      if(input >> value) //PROBLEM HERE!! 
      { 
       Matrix[i][j] = value; 
       cout << "Debug: Check for values in matrix: " << Matrix[i][j] << endl; 
      } 
      else 
       Matrix[i][j] = 0; 
     } 
    } 

    cout << "The values in Matrix after initialization: " << endl; 

Print_result(); 
} 

的問題。我不確定它爲什麼不起作用。

回答

4

你實際上並沒有使用istringstream。我認爲你正在尋找類似的東西,

.. 

istringstream is(input); 
if (is >> value) 
{ 

... 

'is'是從字符串「input」創建的istringstream。

+0

你說得對,我忘了「是」。非常感謝! – Josh

相關問題