Reputation: 155
I'm overloading the extraction operator to read a node which contains a pair of numbers.
As you can see I'm printing a message for the user to know which number they are writing. However when I read from a file the messages will still print. So I want to know if there is a way to check if I'm reading from a file or from the keyboard or if there is another way to avoid printing the messages when I'm reading from a file.
Code:
istream &operator>>( istream &input, nod &X )
{
cout<<"Number 1: ";
input>>X.info1;
cout<<"Number 2 ";
input>>X.info2;
X.next=NULL;
return input;
}
Upvotes: 1
Views: 167
Reputation: 66401
You shouldn't interact with a user at all in the streaming operators.
That's not what they're for, and operator>>
should just read the next object that's in the stream.
Instead, interact first and then construct the object:
nod read_nod()
{
int info1 = 0;
cout << "Number 1: ";
cin >> info1;
int info2 = 0;
cout << "Number 2: ";
cin >> info2;
return nod(info1, info2);
}
or
istream & operator>> (istream &input, nod &X)
{
input >> X.info1;
input >> X.info2;
X.next = NULL;
return input;
}
nod read_nod()
{
cout << "Enter two numbers for the nod: ";
nod n;
cin >> n;
return n;
}
Upvotes: 2