-4
int main()
{
   ifstream inputFileStream;  // declare the input file stream
   int lettersArray[ LIMIT];  // count occurrences of characters
   char c = ' ';              // input character
   int i;                     // loop counter

    string fileName = "MacbethEnglish.txt";

   // open input file and verify
   inputFileStream.open( fileName.c_str());   // Convert C++ string to expected C-style string 
   if( !inputFileStream.is_open()) {
     cout << "Could not find input file.  Exiting..." << endl;
     exit( -1);
   }

   // initialize lettersArray count values 
   for (i=0; i<LIMIT; i++) {
      lettersArray[ i] = 0;
   }

   // Process input one character at at time, echoing input
   // Note that the input skips leading spaces, which is why we don't see
   // them as part of the output.
   cout << "Reading characters from a file... " << endl;
   **while( inputFileStream >> c)** {
      // convert alphabetic input characters to upper case
      if( isalpha( c)) {
          c = toupper( c);
          lettersArray[ c-'A']++; // update letter count, using the character as the index
          // cout << c << " ";    // Takes too long to display when enabled
      }
   }

What is exactly is while( inputFileStream >> c) the bolded line supposed to be doing? I do not understand the condition/or operator. I am trying to change the code from reading a text file to reading a string instead. I am not so sure how to do this. I wanted to create an identical function but changing the functionality for strings instead of txt files.

John
  • 1
  • 1

2 Answers2

0

Operator >> when applied to a stream reads formatted input from the stream and stores it into the variable on the right hand side. The expected format of the input depends on the type of the respective variable. So, if c is of type char, then operator >> will read in a single character, if available. If the stream cannot read the expected input (e.g. if end of file has been reached), the result of the overall operation is false (as a result of converting the resulting stream with an error flag set to a boolean), such that the loop will not be entered any more.

Stephan Lechner
  • 33,675
  • 4
  • 27
  • 49
  • 2
    "the result of the operation is false" - no, the result of the operation is always a stream reference. The while() then tests the state of the stream referred to. –  Oct 15 '18 at 19:34
  • @Neil Butterworth: yes, but in the context of a condition it will be converted to `bool` (and false in this case) – Stephan Lechner Oct 15 '18 at 19:35
  • @NeilButterworth the result of the expression is boolean. So even if the `operator>>` returns a reference to a stream this will be converted to bool (C++14) or implementation defined bool like. – Martin York Oct 15 '18 at 19:36
  • @Martin Yes, I'm aware of that, but if someone asks "What does `cin >> x` do?", then it reads into x and returns a stream reference. –  Oct 15 '18 at 19:37
  • It is worth noting that a formatted stream drops leading space when reading a value using `operator>>` (by default). – Martin York Oct 15 '18 at 19:37
  • @NeilButterworth. Yes that's true. But when they ask what does `while(cin >> x)` do. Then you need to explain the result of the expression will be a bool. – Martin York Oct 15 '18 at 19:39
  • @Martin and the correct explanation is that the >> operator returns a stream reference which is then converted to a bool reflecting the referred to stream's state. –  Oct 15 '18 at 19:40
  • @NeilButterworth Again I agree with you. And Stephen's answers did that (in the original and the updated version). He was talking about the result of the `overall operation` not the result simply the first half of the operation. – Martin York Oct 15 '18 at 19:42
0

Its basically a conversion of the stream to bool after extracting a character. The conversion yields false eg when reaching the end of the file (more specifically, it returns !fail()).

See eg here for the operator<< : https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt

And eg here for the conversion to bool: https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool

Also note that here is almost no difference between reading from a file or from a string if you use a std::stringstream in place of the std::ifstream.

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126