I am basically reading a .txt
file and storing values.
For example:
Student- Mark
Tennis
It will store Mark
into memory as the studentName
.
Now...If it is just
Student-
Tennis
Then it will work fine and produce an error.
However, if the file looks like this
Student-(space)(nothing here)
Tennis
It will store Tennis
into memory as the studentName
, when if fact it should store nothing and produce an error. I use '\n'
character to determine if there is anything after the -
character. This is my code...
istream& operator>> (istream& is, Student& student)
{
is.get(buffer,200,'-');
is.get(ch);
if(is.peek() == '\n')
{
cout << "Nothing there" << endl;
}
is >> ws;
is.getline(student.studentName, 75);
}
I think it is because the is.peek()
is recognizing white space, but then if I try removing white space using is >> ws
, it removes the '\n'
character and still stores Tennis
as the studentName
.
Would really mean a lot if someone could help me solve this problem.
If you want to ignore whitespace but not
'\n'
you can't usestd::ws
as easily: it will skip over all whitespace and aside from' '
the characters'\n'
(newline),'\t'
(tab), and'\r'
(carriage return) are considered whitespace (I think there are actually even a few more). You could redefine what whitespace means for your stream (by replacing the stream'sstd::locale
with a customstd::ctype<char>
facet which has changed idea of what whitespace means) but that's probably a bit more advanced (as far as I can tell, there is about a handful of people who could do that right away; ask about it and I'll answer that question if I notice it, though...). An easier approach is to simply read the tail of the line usingstd::getline()
and see what's in there.Another alternative is create your own manipulator, let's say,
skipspace
, and use that prior to checking for newline: