I'd like to suggest you add some print lines in the important step -- which I think also is a fast&good "debug" method. So that you can find where you wrong easily.
For example in your code, seems textArray
wasn't assigned, so add some print nearby:
while(getline(invFile, lines)) {
cout <<"lines: " << lines << endl; //test enter here
istringstream streamA(lines);
col = 0;
while(streamA >> x) {
cout << "x is" << x; //test if enter here
textArray[row][col] = x;
col++;
}
row++;
}
Through the output, the lines is ok but cout << "x is" << x;
wasn't printed, which means the while(streamA >>x)
condition is false, why?
Go to find the library function called, std::istringstream x
is int type but col 1 value is Apples
, operator <<
will return NULL, it's unreasonable assing Apples
to an int
, till now, found point 1. If have to use int
or float
to store the numbers, use some convert API like atoi
, atof
.
After change x
from int
to string
, got segmentation falut, it's obviously that textArray[2][2] is not enough to store all the information. "Out of range" is the reason of segmentation fault, so make a large array to test continue until passed.