Declaring a variable in is stream?

My question is at line 22-26

I created an is stream and want char c1, c2, c3; to be inputted by the user... what did I miss?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 #include <iostream>
#include "std_lib_facilities.h"

using namespace std;

struct Point{
 int x;
 int y;

Point() :x(0), y(0) { }
Point(int xx, int yy) :x(xx), y(yy) { }

};

ostream& operator<<(ostream& os,Point& p)
{
   os << "(" << p.x << "," << p.y << ")"<<endl;
   return os;
}


istream& operator>>(istream& is, Point& p)
{
   int x;
   int y
   char c1, c2,c3;

    is >> c1 >> x >> c2 >> y >>c3;
    for( c1!= '('|| c2!=','|| c3!= ')')
        {is.clear(ios_base::failbit);
        return is;}
    p =Point(x,y);
    return is;

}


void printv(const vector<Point>& points)
{
    for (int i=0;i<points.size();++i)
    {
        cout<< points[i]<<endl;
    }
}

int main()
try{
    vector<Point>points;
    cout<<"Enter 7 coordinates (x,y) /n";
    for(int i=0;i <7; ++i)
    {
        Point p;
        cin>>p;
        points.push_back(p);
    };

    printv(points);
}




Last edited on
Semicolon at the end of line 25.

Line 29: if, not for

Remove the 'try' on line 47.

Rather than the non-standard #include "std_lib_facilities.h" it would make it easier for others to help you if you used the standard headers: in this case #include <vector>

I also had to make line 15
ostream& operator<<(ostream& os, const Point& p)
but I'm not sure why.



On a further note ...
The more natural thing, if asking for interactive input, is just to input two numbers, not require the user to punctuate them with brackets and commas.
Last edited on
I was trying to practice this exercise form a learn c++ book by stroustrup. That is why I am trying to use I(o)stream.

I'll just skip the chapter if its too complicated. No point in sturggling over this.
Not sure what you are saying, @t009. There's nothing much wrong with your use of streamed input/output per se. If you still have compile-time or run-time errors after addressing those in my post, please state exactly what they are (and show the code that elicits them).
Last edited on
Yeah it works great thank you. I thought it was not working but I had something interfere with compiler and give me error.

I read that it was good practice to skip over things and come back to them later.
Topic archived. No new replies allowed.