Creating i amount of classes

Here is the code i am working with right now. It is going to read in a number of lines of 3 ints that will be the x, y, and radius of a circle. However the number of lines read in isn't set is there a way for me to make a counter and then have it create circle 0 the first time through then circle 1,2,3..... until EOF? If you have any ideas on how to solve this problem I would very much appreciate it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string line;
	ifstream myfile;
	myfile.open("input.txt");
	if (myfile.is_open())
	{
		while (myfile.good())
		{
			getline (myfile,line);
			if(line == "Code 1")
			{
				while(!EOF)
				{
					int i=0;
					double a,b,c;
					myfile>> a;
					myfile>>b;
					myfile>>c;
					Circle2d 'i' c;
				}
		}
	}


Last edited on
what does your 'Circle2d' do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ifstream myfile;
myfile.open("input.txt");
if (myfile.good()) {
     string line;
     getline (myfile,line);
     if(line == "Code 1"){
	  int i=0;//moved here because it increment in the loop.
	  while(!myfile.eof()){
	       double a,b,c;
	       myfile>>a;
	       myfile>>b;
	       myfile>>c;
	       //I dont know the spec of Circle2d
	       //I assume void Circle2d(int i, double x, double y, double radius)
	       Circle2d(i, a, b, c);
	       ++i;
	  }
     }
}
Last edited on
A little more compact:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
getline(myfile, line);

if(line == "Code 1")
{
	int i = 0;

	while(getline(myfile, line))
	{
		double a, b, c;
		myfile >> a >> b >> c;
		Circle2d(i, a, b, c);
		++i;
	}
}
Thanks bbgst, method chain is powerful!

btw, is it ok to call getline before each input?
Topic archived. No new replies allowed.