+ 1

help with c++ ifstreams

so im reading numbers from a txt file that look like: 3 8 1 6 3 5 7 4 9 2 i only need to store 3 in the value of n, without using getline, 3 is what i read first to make the 3x3 matrix below when i use my while loop: while(read >> n){ cout << n << endl; } it displays all the numbers an i only need n = 3 how can i get it to only read 3?

25th May 2018, 8:55 PM
Baric
Baric - avatar
8 Answers
+ 4
Baric You will need to do more work,but, you're welcome!
25th May 2018, 10:30 PM
Manual
Manual - avatar
+ 3
/*bad practice using auto but try this picked fileData instead of n */ for( auto fileData : read){ try{ if(atoi(fileData) == 3){ cout << fileData << endl; } } catch(...){} }
25th May 2018, 9:13 PM
Manual
Manual - avatar
+ 3
Try it see what happens.
25th May 2018, 9:22 PM
Manual
Manual - avatar
+ 3
If you want to just read 3, and you can be sure that 3 is on the first line, assuming inData is your ifstream object, inData >> n; Yep.
26th May 2018, 3:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
still a bit confused that's pretty advanced, havent gone over that in class but it helped :)
25th May 2018, 10:33 PM
Baric
Baric - avatar
+ 1
thank you
25th May 2018, 9:23 PM
Baric
Baric - avatar
0
will this only work for 3 because I need to run it for 5x5 matrix an 7x7 an 9x9 all in the same txt file 3x3 is just the first one
25th May 2018, 9:16 PM
Baric
Baric - avatar
0
i needed to set it to a function in the while while(read >> n){ loadnumbers(array[][], size); } void loadnumbers(int n, int array[size][size]){ for (int row = 0; row < n; row++){ for(int col = 0; col < n; col++){ read >> array[row][col] } } }
5th Jun 2018, 2:17 AM
Baric
Baric - avatar