how can i read integers from an usual text in C++

Hi,I'm so new to c++ so please be easy on me. Now,I have to read integers from a text file like this;

3
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5

i want first two integers in free variables like height is 3 and width is 7,and for other i want to put them into a double array.As long as I tried to do as i read so many questions and comments about problems like this, I could not make it work..I need some suggestions maybe some codes to work it out since as i mentioned above I am so noob about c++. Thank you for your time

here is my newbie code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <stack>
#include <stdlib.h>
#include <vector>
#include <fstream>

using namespace std;


int main(){

	ifstream in;
	in.open("input");
	vector<vector<int> > floor;
	int height;
	in >> height;
	int width;
	in >> width;
	for(int i=0;i<height;i++){
		for(int j=0;j<width;j++)
		{
			in >> floor[j][i];
                        cout << floor[j][i] // in order to check whether it is working or not
		}
	}
	return 0;

}
Last edited on
You need to expand a vector before you can put something in it. You could set it's size in the constructor (except that on line 14 you don't know what it is yet). You should use the resize method floor.resize(width); before the for loop. You could also resize all vectors floor[i] in the first for loop, or you could read integers from "in" to a temporary variable and then push_back it into floor[i].
Topic archived. No new replies allowed.