Declare your integer array and an ifstream:
int A[333];
ifstream input;
Skip the first 14 lines using getline to grab the line up to the newline and then do nothing with it. You've now reached the 15th line.
Set up a loop and read in the 333 integers:
for (int i = 0; i < 333; i++)
input >> A[i];
Close the ifstream.
Note that array indexes should start with 0, not 1.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
You opened filein and you're reading from cin.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Ooooh, what a stupid mistake.
Thanks a lot, man. It now works fine.
I just have one last question:
If that 15th line contains "value=" and then the numbers (with a space between each 2 numbers). How can I skip those initial characters, and begin reading and storing my numbers in A[]?
So, to make it more clear, in my .txt file, the 15th line looks like this for example:
value= 1 22 434 3 100
And I want to have an array of integers A[7], where:
A[1]=1
A[2]=22
A[3]=434
A[4]=3
A[5]=100
How can that be done, please?
If you know FOR SURE that there is NO space between "value" and "=" and that there IS a space between "=" and the first number, just read "value=" into a string and throw it away before you read in the numbers:
string throwAway;
filein >> throwAway; // read in "value="
for (int i = 0; i < 333; i++)
filein >> A[i]; // read in numbers
Again, this only works if the assumptions listed above are valid.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
I'm really sorry guys for the continuous disturbance, but on another thought, I found that my .txt file, its 15th line is in the form of:
value =1 22 434 3 100
Which means that there is a space between the word "value", and "=", while there is no space between "=" and the first number that I want to read.
Now, if I read 2 throwaway strings, the first number is read with the "=" in the second string, and so, the first number is not read in A[0]. How can that be solved in this case?
Thanks in advance.
You can read in a character at a time using the get command. Check to see whether you just read in an equals sign. If not, read in another character. Once you have read in the equals sign, go on to read the numbers, as before, with a for-loop: http://www.cplusplus.com/reference/iostream/istream/get/
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711