How about this?
while ( cin >> x >> y )
What is an example of your input?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Do you want a vector of points instead? That is,
#include <iostream>
#include <vector>
using namespace std;
class point
{
int x, y, z;
public:
point (int xx = 0, int yy = 0, int zz = 0): x (xx), y (yy), z (zz) {}
bool get()
{
cout << "Type twee integere getallen in per keer: " << endl;
return cin >> x >> y;
}
void print()
{
cout << x << "," << y << endl;
}
};
class something
{
vector<point> s;
public:
something()
{
point u;
while ( u.get() )
{
s.push_back(u);
}
}
void print()
{
for ( int n = s.size(), z = 0; z < n; z++ )
s[z].print();
}
};
int main()
{
something interesting;
interesting.print();
cin.get();
return 0;
}
/* my output
Type twee integere getallen in per keer:
1 2 3 4 5 6 stop
Type twee integere getallen in per keer:
Type twee integere getallen in per keer:
Type twee integere getallen in per keer:
1,2
3,4
5,6
*/
>By the way, have you heard anything from Narue, strange for her to stay away for so long
[voice=Sgt. Shultz]I know nothing.[/voice]
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Why do people like you, Narue, Dave,... make it all look so simple, pfff, life isn't fair you know LOL :lol:
Practice, practice, [failure, failure, failure << me at least>> ], practice...Please, don't missunderstand me, I really appreciate your help, but, I'd like to try the exersize first, if I really am not able to solve it, after getting tips, hints, examples, from you, Dave, Narue, Vegaseat, Asif, 1OOOBHP,... I'll ask you to help me out with the solution ;)I generally don't try to provide complete solutions to exact questions, especially homework. But sometimes it's just easier for me to try to explain what I'm thinking with code. Or I feel that posting a correct method ofwhatever is much better than "STFW for a (likely bad) example". And since the web is awash with bad examples, I try to make sure I'm posting only a correct bit of compilable code that demonstrates some portion of an answer; or a different approach to doing the same thing.
It's sort of a "the more good code that people can see, the more likely they will be to become good coders" type of thing -- and in the process of trying to tell someone else how to do something, you often end up learning a heckuva lot more than than you expect to.
Lather, rinse, repeat... and keep trying to get better.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
It would be much easier to answer if you didn't snip it to ... all over the place. I don't really care to guess at what goes into all the ...'s and attempt to get something to compile.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
I get an error message (trimmed):
'operator<' not implemented in type 'point'With that and your previous reply, in the first case you have a vector ofints? And the second you have a vector of points? I believe the reason might be that operator< is implemented for ints.
[edit]Tought you wouldn't need the total code and therefore posted only those parts.Little things likevector vs vector often lurk in ..., but many times that is where the answer is.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
What's that got to do with
std::sort(points.begin(), points.end());
As I said in the previous post, I'm not working with the operator functions just yet.
I believestd::sort uses operator< to do the sorting.
class point
{
// ... (sorry I mean these are just the couple changes to what you have)
bool operator< (const point &b)
{
return x < b.x || (x == b.x && y < b.y);
}
friend bool operator< (const point &a, const point &b);
};
bool operator< (const point &a, const point &b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
Suddenly it compiles.
[Reminder: C++ is not my native language.]
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
How would you perform a sort if you didn't have a comparison function?
How would the compiler know if widget a is supposed to be before widget b?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314