I’m trying to read inputs from a file which are marked with {1…9} or X. I need to separate the values and store them in a vector properly. I’m using the “sstringstream” to help me do so:
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();
}
The problem occurs in the if statement, when there is an integer in “input” it doesn’t execute the if statement. I’m not sure why it’s not working.
You’re not actually using the istringstream. I think you’re looking for something like,
Where ‘is’ is the istringstream created from the string “input”.