I am trying read in a text file with a 10 by 2 array of values into my program and I would require the program to assign it back into another array of 10 by 2. The columns are separated by one tab t.
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
As the input is coming from a text file, I would have to convert it into a string and the point where I am stuck is converting the string into an array.
The problem I am having is that when the input file is converted into a string, the tab or whitespace is ignored and the string becomes "11" for the first line.
is there anyway that I can split up the "11" and store the first "1" into array[0][0] and second "1" into array[0][1] and so on...? this array will then be used to perform further calculations.
Below is the code I have written so far. Could anyone kindly please provide a solution for this problem?
Use the >> operator instead of getline - it stops at whitespaces. If you only want the input to stop at tabs and not at other space characters, use getline and provide the third argument as 't' (the third argument of getline is the delimiter character, it's 'n' by default).
EDIT: Anyone know why you have to escape backslashes in this forums?
I have tried using the delim slash t as the third argument and manage to extract the value before the tab which is just "1". May I know how do I go about extracting the 2nd "1" from the next column before proceeding to the next row???
I have came up with the code below and the output is exactly what I wanted but it seems to be skipping the first number in the front. I have debug it and watch the variables change but I still do not understand why it skips the first number...
Anyone have any idea why is it skipping the first number??
The output for the below code is:
Results[0][0]: 1
Results[0][1]: 2 //this output should still be 1
Results[1][0]: 2
Results[1][1]: 3
Results[2][0]: 3
Results[2][1]: 4
Results[3][0]: 4