input stream problem v2.0

Before several hours i posted this question, i got good answer by tipaye and closed the topic but turned out that i still wasnt able to find the solution. So here it goes again

This is original thread -- > http://www.cplusplus.com/forum/beginner/157339/

But for easier reading ill post orginal question(exercise from book) here again
Modify the ... program to make the input stream an explicit parameter. rather than simply using cin. Also give Token_stream constructor an istream& parameter so that when we figure out how to make our own streams (attached to files) we can use the ... program for those. Hint : Dont try to copy an istream


This was original code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

class Point{
public:
	int x;
	int y;
	Point() : x(0), y(0) {}
};

class Token_stream{
	Point point;
	// ...
public:
	Point get_input();
	// ...
};

Point Token_stream::get_input(){
	std::cout << "Enter 2 ints ";
	std::cin >> point.x >> point.y;
	return point;
}

int main(){

	Token_stream ts;
	Point p;
	p=  ts.get_input();
	std::cout << "x = " << p.x << std::endl
	          << "y = " << p.y << std::endl;
}


This is what tipaye suggested me to do and tnx him for that
Hints:
Add a member of type std::istream reference to Token_stream
Write a constructor that takes an istream reference
Initialise member in constructor
In get_input(), use member instead of std::cin


This is what i did
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

class Point{
public:
	int x;
	int y;
	Point() : x(0), y(0) {}
};

class Token_stream{
	Point point;
	std::istream& stream;// member of std::istream&
public:
	Point get_input();
	Token_stream(std::istream& x) : point(), stream(x){}   //constructor i made
};

Point Token_stream::get_input(){
	
	std::cout << "Enter 2 ints ";
	stream >> point.x >> point.y; // in get_input use member instead of std::cin
	return point;
}

int main(){
	std::istream input();   // ??
	Token_stream ts(input());  // ??

	Point p;
	p=  ts.get_input();
	std::cout << "x = " << p.x << std::endl
			  << "y = " << p.y << std::endl;
}

I think that everything is all right except for passing an argument for Token_stream ts(??) when i define it in main(). Dont really understand what to give it as argument. Any help appreciated.
Last edited on
The constructor is: Token_stream( std::istream & )

It wants a reference to a std::istream object. What was the istream object that you were using in the earlier program? std::cin

What other istream types are common? std::ifstream and std::istringstream
I could write:
1
2
std::ifstream infile;
Token_stream ts( infile );

(But you have to study the ifstream to understand that it is missing something.)


Lines 26 and 27 remind me of Scott Meyer's "most vexing parse". See http://en.wikipedia.org/wiki/Most_vexing_parse

Why does the Token_stream have member 'point'? It does not seem to need it.

Lets suppose that the program does read the input from a file. Does it make sense to shout to cout on line 20?
UPDATE:
1
2
3
4
5
6
7
8
int main(){
	
	Token_stream ts(std::cin); 
	Point p;
	p=  ts.get_input();
	std::cout << "x = " << p.x << std::endl
			  << "y = " << p.y << std::endl;
}

So this worked. I hope this is how solution should look like for my exercise.
Ohh dint see u posted before so tnx a lot :) With this example i just tryed to recreate calculator programs part in the book what i should be changing and create istream as Token_stream parameter. So no worries about cout's, they are just for fun when i check out if this is actually working :)

But tnx a lot for reply again. Appreciate it!
Last edited on
Topic archived. No new replies allowed.