User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2004
Location: Belgium
Posts: 405
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Class with use of <vector>

  #1  
May 24th, 2005
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
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Location: Belgium
Posts: 405
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #2  
May 24th, 2005
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
Reply With Quote  
Join Date: Apr 2004
Posts: 3,418
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 15
Solved Threads: 137
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class with use of <vector>

  #3  
May 24th, 2005
How about this?
while ( cin >> x >> y )
What is an example of your input?
Last edited by Dave Sinkula : May 24th, 2005 at 9:54 am. Reason: Do'h! Pokey fingers.
Reply With Quote  
Join Date: Sep 2004
Location: Belgium
Posts: 405
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #4  
May 24th, 2005
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,
 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
Reply With Quote  
Join Date: Apr 2004
Posts: 3,418
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 15
Solved Threads: 137
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class with use of <vector>

  #5  
May 24th, 2005
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]
Reply With Quote  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Class with use of <vector>

  #6  
May 24th, 2005
>> 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):
#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';
}
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:
#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';
}
But that's not as clean, so unless somebody is looking, go for the first option.
Reply With Quote  
Join Date: Sep 2004
Location: Belgium
Posts: 405
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #7  
May 24th, 2005
>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:
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.
(<-- so far so good, no problem)

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]
Reply With Quote  
Join Date: Sep 2004
Location: Belgium
Posts: 405
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #8  
May 24th, 2005
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 :!:
Reply With Quote  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Class with use of <vector>

  #9  
May 24th, 2005
>> Define a class "point" with two datamembers, x and y.
struct point {
  int x, y;
};
>> Use the STL-container vector to stack a row of point-objects.
#include <vector>

std::vector<point> points;
>> 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.
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);
}
>> Define the operators == and < for two point-objects
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);
}
>> 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:
std::sort(points.begin(), points.end());
Reply With Quote  
Join Date: Sep 2004
Location: Belgium
Posts: 405
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #10  
May 24th, 2005
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:
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC