Create objects inside a do-while with contiguos name

Hi. I have a do-while inside a switch. Before that I had the definition of a class. So, the thing is that I need to instance objects in the loop with the parameters that the user enter. (EX: c1 with x=3,5 and y=3,6; c2 with x=numberthatuserenter y =numberthatuserenter, etc until the user enter that he don't want to créate any more)

So I Really don't know how to créate this objects with contiguos name (c1,c2,...)

Thanks a lot and sorry for my bad english.
1
2
3
4
5
6
7
8
9
10
11
//...
  	case 1:
			int i = 0;
			do {
/* créate instances like c1, c2, c3,... here */
				

			} while (//...);
			break;
//...
Sounds like you would like an array of some sort. Go look up arrays and possibly dynamic allocation depending on what you want to do with it.
Yes maybe I can use an array. I didn't think on that. The goal is to "keep" (only in memory for now) some points of a coordinate axis that the users entered.

Then I want to compare everyone of these "objects" (the x parameters and the y parameters of each one) to know the distance, but this is another subject that i will think how to do in a future.

So, with this new information, do you think that I can use an array?

Thanks a lot.
Last edited on
Yes, that sounds exactly like what you want an array for.
Hi,

Consider using a std::vector<std::pair>

std::vector because one is not limited to, or has to worry about how many objects. One can push_back values or brace initialise them.

std::pair holds two values - your X and Y ordinates. If you wanted to do 3d, you could use std::tuple

Or std::vector<std::array<double, 3>>

http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
http://www.cplusplus.com/reference/utility/pair/
http://en.cppreference.com/w/cpp/utility/pair
http://www.cplusplus.com/reference/tuple/tuple/
http://en.cppreference.com/w/cpp/utility/tuple
http://www.cplusplus.com/reference/array/array/
http://en.cppreference.com/w/cpp/container/array


Edit:

Or use a vector of a point struct

1
2
3
4
5
6
struct Point2D {
   double x;
   double y;
};

std::vector<Point2D> PointData;


I made Point2D a struct, with the idea that PointData would be a private member of a class.

Otherwise, you could make Point2D a full blown class.

Also, avoid putting too much code into a case: unless it's a one-liner, call a function instead. This helps with readability and aids understanding because it is a form of abstraction.

Good Luck !!
Last edited on
Thanks a lot to both of you. It seems i have to read a lot to implement the solutions XD. good luck and thanks again.
Topic archived. No new replies allowed.