I have a problem with overloading >> operator for string class;
here is my class:
class str
{
char s[250];
public:
friend istream& operator >> (istream& is, str& a);
friend ostream& operator << (ostream& os, str& a);
friend str operator + (str a, str b);
str * operator = (str a);
friend int operator == (str a, str b);
friend int operator != (str a, str b);
friend int operator > (str a, str b);
friend int operator < (str a, str b);
friend int operator >= (str a, str b);
friend int operator <= (str a, str b);
};
and here is overloaded operator:
istream& operator >> (istream& in, str& a)
{
in>>a.s;
return in;
}
the problem is that it reads the string only to first space(only one word from sentence).
I solved it. Found the answer on dreamincode 😀
The behavior for
operator>>is to read until the first whitespace character. Change your function to the following: