Say for example i wish to cin a set of 6 numbers. But the user will cin them in this format.
[1 2 3 4 5 6]
Is there a way to design it so that the [ ] are ignored or not placed into the array which i created?
Like if were to enter 1 2 3 4 5 6 everything would be fine. If i were to enter [1 2 3 4 5 6] obviously i would have problems. Is there a way to define that [] should be ignored or not even inputted into the array in input?
Below im using a overloading operator where i cin information. The first two values are just the dimensions of my 2d array. So if i was to enter [2 2 5 3 5 3] it would only input the values after 2 2 which are the size of the array i setup. But i need to handle these symbols [ ]
so they dont conflict. What would be the best way to fix this up?
friend istream& operator>>(istream& is, Array<T> &array)
{
int rowX, colY;
is >> rowX;
is >> colY;
array.create(rowX, colY);
for(int i = 0; i<array.numRowX(); i++)
for(int j = 0; j<array.numColY(); j++)
{
T data;
is >> data;
array.setarray(i, j, data);
}
return is;
}
You can use a variable to read input which you want to ignore. If you are simply skipping single characters, something like this should work:
char ignoreChar;
is >> ignoreChar; // Should be a '['
// for loop goes here
is >> ignore; // Shoudl be a ']'
You might want to add if statements to make sure the ignored character is in fact what you expect it to be.
Alternatively, you can read in a whole line as a string and extract the information you want from there. This is called string parsing.