understanding code

Hi, I am currently learning about loops in c++. Recently I came across a topic called 'end of file' condition. The following code was provided as an example in the book.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// textin3.cpp -- reading chars to end of file
#include <iostream>
int main()
{
  using namespace std;
  char ch;
  int count = 0;  
  cin.get(ch);   // attempt to read char
  while (cin.fail() == false)  // test for EOF
    {
      cout << ch;    // echo character
      ++count;
      cin.get(ch);  // attempt to read another char
    }
  cout << endl << count << " characters read\n";
  return 0;
}


I have two doubts in this example:
1) What does cin.get(ch) do -- does it just take the next character?
2) I didn't understand the while loop condition (cin.fail() == false), what does it mean / try to do?
Thanks in advance.
This site has some good documentation which will help with technical questions like those: 1) yes, 2) stop loop at EOF (or other file failure).

I don't think that's the best example I've ever seen, but it is better than most. The point is to try to read input, break the loop immediately after the attempt if failed, else process the (valid) input and loop.

Hope this helps.
I went through another code example and it used:

#include <iostream>
int main(void)
{
using namespace std;
int ch;
int count = 0;
while ((ch = cin.get()) != EOF)
{
cout.put(char(ch));
++count;
}
cout << endl << count << " characters read\n";
return 0
};

when i run it :
textin4.cpp: In function ‘int main()’:
textin4.cpp:9:30: error: ‘EOF’ was not declared in this scope

@Duoas: what i understood by 2)stop loop at EOF -- is basically stop the loop when no more characters are available, is that right?

can you please explain the while loop condition and the cout statement in its body?

is learning about EOF important? I don't the the idea behind this.
Last edited on
@Duoas: what i understood by 2)stop loop at EOF -- is basically stop the loop when no more characters are available, is that right?

yes, or when the input is invalid.

The while loop assigns the value of the character extracted and assigns it to ch. if there is no character to be extracted in the stream, EOF is returned. if this condition is met, the loop ceases to function.
http://www.cplusplus.com/reference/istream/istream/get/

the puut member function is used to output a character. so we ypecast the int to a char, and add it to the output stream.
http://www.cplusplus.com/reference/ostream/ostream/put/

Aceix.
Alright, so ch takes the character value until the EOF is reached.

And, the 'int ch' is converted to a char and then displayed using cout.put(char(ch));

In the first code example
while (cin.fail() == false)
checks whether it is the end of the file is reached, right? And continue if it hasn't.

It does not check for only EOF. For only EOF checking use the eof(); member function.
Check: http://www.cplusplus.com/reference/ios/ios/fail/
So it keeps taking input till the end of file? I am getting confused with the eof thing, is it important to know this?
A basic input loop should work like this:

while (true)
{
  try to get input;
  if (not successful) break;
  do something with the input;
}

The extraction operator in C++ makes code that does this very natural looking:

1
2
3
4
5
char c;
while (cin >> c)
{
  ...
}

This works because the result of cin >> c is the istream object itself, which is then tested for a boolean value, which the istream overloads to mean "is good()?". If the stream is not good() (because of an input error or EOF), then the loop quits.

Another way to do it is:
1
2
3
4
5
int c;
while (cin.get( c ))
{
  ...
}

Again, the loop condition is really doing two things: attempting to get a single character from the stream and returning the istream object, which is then tested for good()ness. Had anything gone wrong when attempting input, the stream is not good() and the loop terminates immediately.


EOF is short for "end of file", which is just a way of saying that there isn't any more input to be read.

For example, given the string "abc", if we get one character at a time from it we get 'a', then 'b', then 'c', then... then there are no more characters. We've hit the end of the string. That's exactly what is happening at EOF -- end of file.


BTW, the two code fragments above are not quite equivalent. The >> operator skips leading whitespace, while cin.get() does not. You can make them work equivalently by turning on the "no skip whitespace" flag:

1
2
3
4
5
6
cin >> noskipws;
char c;
while (cin >> c)
{
  ...
}

There are still minor differences between them, but not that you can see if you were to use it this way.

Hope this helps.
@Duoas:
while (cin.fail() == false) // in the first code example
the loop condition means that input must be taken till fail() (here it fails when it reaches the end of file or a specific symbol) is false.

while ((ch = cin.get()) != EOF)
the loop condition means that input must be taken till end of file is not reached and then is stored in an int variable.

Thanks I think I get it now!
:O)
Topic archived. No new replies allowed.