•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,018 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,696 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 3568 | Replies: 30
![]() |
Hello ladies and gents,
Wanted to make this part of an exercise in wich I enter each time two integers into an array for wich I have to use STL container vector.
Ive got this sofar, but getting an error message when I entered two numbers and the exe shuts down? I'm almost certain it's got to do with the
But, not sure about it and even if, don't know how to solve it?
What Ive got sofar in total is this:
Could someone please point me in the right direction, thank you
Wanted to make this part of an exercise in wich I enter each time two integers into an array for wich I have to use STL container vector.
Ive got this sofar, but getting an error message when I entered two numbers and the exe shuts down? I'm almost certain it's got to do with the
while (cin>> x >>" ">> y, !cin.fail())
But, not sure about it and even if, don't know how to solve it?
What Ive got sofar in total is this:
#include <iostream>
#include <vector>
using namespace std;
class point
{
private:
int x, y, z;
public:
point (int xx = 0, int yy = 0, int zz = 0): x (xx), y (yy), z (zz) {}
vector<int> s, t;
void numbers()
{
while (cin>> x >>" ">> y, !cin.fail())
{
s.push_back(x);
s.push_back(y);
}
}
void print()
{
int n = s.size();
for (z = 0; z < n; z++)
cout<< s[z] <<endl;
}
};
int main()
{
point u;
cout<<"Type twee integere getallen in per keer: "<<endl;
u.numbers();
u.print();
cin.get();
return 0;
}Could someone please point me in the right direction, thank you
Found it, it's this wich was causing the problem
Still, if someone might want to have a look at the part of code that I wrote allready and give some hints or suggestions to improve it, I'd very much appreciate it
>>" ">>
Still, if someone might want to have a look at the part of code that I wrote allready and give some hints or suggestions to improve it, I'd very much appreciate it
How about this? What is an example of your input?
while ( cin >> x >> y )
Last edited by Dave Sinkula : May 24th, 2005 at 9:54 am. Reason: Do'h! Pokey fingers.
Hi Dave,
Ive allready changed the cin part
Input for example:
10 20
30 40
50 60
stop
Output should be and is now:
10
20
30
40
50
60
This is just a small part of the exersize tough, have to include the operators == and <.
But, the exersize also talkes about two point-objects?? I'm I correct in thinking that this is when a class object has two datamembers?
Something like this,
If you don't understand what I'm trying to say, then I'll translate the entire exersize.
By the way, have you heard anything from Narue, strange for her to stay away for so long
Ive allready changed the cin part
Input for example:
10 20
30 40
50 60
stop
Output should be and is now:
10
20
30
40
50
60
This is just a small part of the exersize tough, have to include the operators == and <.
But, the exersize also talkes about two point-objects?? I'm I correct in thinking that this is when a class object has two datamembers?
Something like this,
class point
{
point (int xx = 0, int yy = 0): x (xx), y (yy){} --> two datamembers x and y.
...
};
int main()
{
point u;--> one class object
...
}If you don't understand what I'm trying to say, then I'll translate the entire exersize.
By the way, have you heard anything from Narue, strange for her to stay away for so long
Do you want a vector of points instead? That is, >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]
#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
*/[voice=Sgt. Shultz]I know nothing.[/voice]
>> while (cin>> x >>" ">> y, !cin.fail())
Since you already know what the problem was, I'll tell you why it's a problem, just to be thorough. cin >> is strictly for formatted input. It doesn't accept formatting strings and it already skips whitespace by default. If you want to skip precise strings then you can do this (nonportable):
It's not portable because you technically aren't allowed to add stuff to the std namespace. You can do it portably by writing a manipulator:
But that's not as clean, so unless somebody is looking, go for the first option.
Since you already know what the problem was, I'll tell you why it's a problem, just to be thorough. cin >> is strictly for formatted input. It doesn't accept formatting strings and it already skips whitespace by default. If you want to skip precise strings then you can do this (nonportable):
#include <iostream>
namespace std {
template <typename CharT, typename Traits>
basic_istream<CharT, Traits>&
operator>>(basic_istream<CharT, Traits>& in, const char *fmt)
{
while (in.good() && *fmt != '\0') {
if (in.get() != *fmt++)
in.setstate(ios::failbit);
}
return in;
}
}
int main()
{
int a, b;
std::cout << "Enter two numbers separated by =-=: ";
std::cin >> a >> "=-=" >> b;
std::cout << "a: " << a << '\n' << "b: " << b << '\n';
}#include <iostream>
class skip {
const char *_fmt;
public:
skip(const char *fmt): _fmt(fmt) {}
template <typename CharT, typename Traits>
friend std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& in, const skip& sk)
{
const char *tmp = sk._fmt;
while (in.good() && *tmp != '\0') {
if (in.get() != *tmp++)
in.setstate(std::ios::failbit);
}
return in;
}
};
int main()
{
int a, b;
std::cout << "Enter two numbers separated by =-=: ";
std::cin >> a >> skip("=-=") >> b;
std::cout << "a: " << a << '\n' << "b: " << b << '\n';
}
>Do you want a vector of points instead?
Euh, no, don't think I need that, but thanks for the example, why would you use this?
The translation of the exersize is:
(<-- so far so good, no problem)
But this part of the exersize, I don't really understand what I'm supposed to do:
I understand that those operators "== and <" are linked towards this: "p.x < q.x || (p.x == q.x && p.y < q.y)" But, don't know how this should get incorporated into a piece of code?
>[voice=Sgt. Shultz]I know nothing.[/voice]
LOL :lol:
[voice=Homer].....DOH,.. ssstupid policeman[/voice]
Euh, no, don't think I need that, but thanks for the example, why would you use this?
The translation of the exersize is:
•
•
•
•
Define a class "point" with two datamembers, x and y. Use the STL-container vector to stack a row of point-objects. Enter them by using the keyboard by two(pairs), x and y. It's not known how many (pairs) there will be entered. Use a non numeric character to stop the input of the (pairs) x and y.
But this part of the exersize, I don't really understand what I'm supposed to do:
•
•
•
•
Define the operators == and < for two point-objects in wich p<q has the following meaning: p.x < q.x || (p.x == q.x && p.y < q.y)
Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y by using the defined order operator <.
I understand that those operators "== and <" are linked towards this: "p.x < q.x || (p.x == q.x && p.y < q.y)" But, don't know how this should get incorporated into a piece of code?
>[voice=Sgt. Shultz]I know nothing.[/voice]
LOL :lol:
[voice=Homer].....DOH,.. ssstupid policeman[/voice]
Hey Dogtree,
Thanks for the explanation man
But euh, ... if you don't mind, I'm going to skip it in this exersize since it's not being asked to implement it, I'm having a hard time allready getting it to do what it should without incorporating some more code :cheesy:
But, greatly appreciated man :!:
Thanks for the explanation man
But euh, ... if you don't mind, I'm going to skip it in this exersize since it's not being asked to implement it, I'm having a hard time allready getting it to do what it should without incorporating some more code :cheesy:
But, greatly appreciated man :!:
>> Define a class "point" with two datamembers, x and y.
>> Use the STL-container vector to stack a row of point-objects.
>> Enter them by using the keyboard by two(pairs), x and y.
>> Use a non numeric character to stop the input of the (pairs) x and y.
>> Define the operators == and < for two point-objects
>> Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y
>> by using the defined order operator <.
std::sort uses operator< by default, so you can simply do this:
struct point {
int x, y;
};#include <vector> std::vector<point> points;
>> Use a non numeric character to stop the input of the (pairs) x and y.
while (std::cin.peek() != 'q') {
point item;
int x, y;
if (!(std::cin >> x >> y))
break;
item.x = x;
item.y = y;
points.push_back(item);
}bool operator==(const point& a, const point& b)
{
return a.x == b.x && a.y == b.y;
}
bool operator<(const point& a, const point& b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}>> by using the defined order operator <.
std::sort uses operator< by default, so you can simply do this:
std::sort(points.begin(), points.end());
Why do people like you, Narue, Dave,... make it all look so simple, pfff, life isn't fair you know LOL :lol:
Thanks Dogtree, though, have one favor to ask you, if possible, could you give me some hints and clues in the future or even examples wich are related to the exersize instead of the solution because, that way, I'll learn much more then by simply getting the solution on a silver platter.
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
Anyway, thanks for the solution, this way, I'll get to the next chapter much faster :cheesy:
Thanks Dogtree, though, have one favor to ask you, if possible, could you give me some hints and clues in the future or even examples wich are related to the exersize instead of the solution because, that way, I'll learn much more then by simply getting the solution on a silver platter.
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
Anyway, thanks for the solution, this way, I'll get to the next chapter much faster :cheesy:
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- C++ derived class virtual functions (C++)
- Reading an input file as a class memeber function (C++)
- Class Vectorization requirements (C++)
- Error pushing pointers to a vector (C++)
- Is ArrayList Better than Vector (Java)
- 6 Line class -> Me pounding head into wall (C)
Other Threads in the C++ Forum
- Previous Thread: Draw a house into a window in C++ (?!)
- Next Thread: How bad is this?



Linear Mode