What I need to do:
Read a text file of type ifstream called textFile
one character ch
at a time until ch
is equal to a single quotation mark '
. If ch is never equal to a quotation mark, print out fail and break out of the loop.
//read a character from the file into ch;
textFile.get(ch);
// while ch is not a single quote
while (ch != '\'')
{
//read in another character
textFile.get(c);
if (textFile.peek(), textFile.eof())
{
cout << "FAIL";
break;
}
}
The textFile.txt that I am reading from has NO single quotes, therefore the output should be FAIL.
However when I print it, it prints fail twice. Any help is appreciated
ifstream::get(char& c)
will return the ifstream
object that is used to read, and it can be used as condition to check if the reading was successful. Use it.
Your code should be like this:
char c, ch;
// while ch is not a single quote
do
{
//read in another character
// you can use ch directly here and remove the assignment ch = c; below if you want
if(!textFile.get(c))
{
cout << "FAIL";
break;
}
ch = c;
} while(ch != '\'');