reading from input stream

This is a discussion on reading from input stream within the C++ Programming forums, part of the General Programming Boards category; Hi, inspired with my recent problems and some other post I found on boards I need explanation for this: Code: ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    reading from input stream

    Hi, inspired with my recent problems and some other post I found on boards I need explanation for this:

    Code:
    #include<iostream>
    using namespace std;
    
    int main()     
    {
         int x;
    
         while (cin>>x)
         {
         }
    
         return 0;
    }
    I test this program by entering following:
    1 2 3 4 5 ^Z
    all in one line delimited with spaces, and loop doesn't exit.
    Strange. I use Dev-Cpp compiler.
    Again I test with the following
    1 2 3 4 5 ^T
    where ^T is (Ctrl+T)
    end program exits.

    Can someone test on other compiler an let me know if the behavior is same or not?
    Why loop and program exit when found Ctrl+T in input stream and not Ctrl+Z when it is supposed to be EOF in windows (I use win XP)?

    Thanks

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    when i press ^T or ^(any other letter) it exits fine

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Which compiler are you using?

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I modified your code a little bit, and ran it on win98 with VC++6
    Code:
    #include<iostream>
    using namespace std;
    
    int main()     
    {
     int x;
     int count = 1;
     
    	 while (cin>>x)
    	 {
    		 cout<<endl<<endl;
    		 cout<<count++<<"----------"<<endl;
    		 
    		 if(cin.eof())
    			 cout<<"eofbit error flag set"<<endl;
    		 if(cin.bad())
    			 cout<<"badbit error flag set"<<endl;
    		 if(cin.fail())
    			 cout<<"failbit or badbit error flag set"<<endl;
    	}
    
    	return 0;
    }
    Results:
    Code:
    ^Z and no space after 5:
    
    1 2 3 4 5
    1----------
    
    
    2----------
    
    
    3----------
    
    
    4----------
    
    
    5----------
    eofbit error flag set
    Press any key to continue
    Code:
    ^Z with a space after the 5:
    
    1 2 3 4 5
    1----------
    
    
    2----------
    
    
    3----------
    
    
    4----------
    
    
    5----------
    (hangs waiting for more input)
    
    6  (space after 6)
    6----------
    7(no space after 7)
    7----------
    eofbit error flag set
    Press any key to continue
    Code:
    ^T  and no space after the 5:
    
    1 2 3 4 5
    Ctrl/T just inserts a character in the input stream.

    If you have a space after the input, then cin>> will read the last number and terminate it's read when it hits the whitespace, and the operator>> is programmed to leave the whitespace in the stream. The operator>> also skips leading whitespace, so the next time through the loop it can't find any data, so it hangs waiting for input.

    If there is no space after the last number, then when cin>> tries to read the last number, it can't finish its read because no whitespace is found, and somehow that causes the eofbit error flag to be set rather than hanging and waiting for more input.
    Last edited by 7stud; 05-01-2005 at 11:24 AM.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Ok, I know that Crtl+T will cause readin to fail, and that's why loop exits. But here is my output of your code:
    Input:
    1 2 3 4 5^Z (no space between 5 and Ctrl+Z)

    1----------


    2----------


    3----------


    4----------

    waiting for input....

    if I press enter result is:

    5----------

    and again wait....

    Another Ctrl+Z will exit loop.


    New test:

    Input:
    1 2 3 4 5 ^Z (space between 5 and Ctrl+Z)
    Output:

    1----------


    2----------


    3----------


    4----------


    5----------

    and waits...

    Next Ctrl+Z or Ctrl+T or just 'g' will exit loop without any of that error messages displayed.

    Compiler Dev-Cpp 4.9.9.2 OS Win XP

    Now what can I conclude from this output?

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Now what can I conclude from this output?

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Exactly, my friend
    I came to the same conclusion...

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    My book says windows doesn't always recognize ^Z as an end of file marker and it may require two ^Z's.

    Here is an attempt at an explanation:

    This is what you have in the stream after the 4 is displayed(pretend their are no spaces--I'm adding them for readability):

    5 ^Z

    On the next read, cin>> hangs waiting for more input. I would say that means the ^Z is not being interpreted as an end of file marker and it is not whitespace, so cin>> can't finish it's read and hangs. Subsequently, when you hit enter, the stream looks like this:

    5 ^Z NL

    So, cin>> should read in 5^Z and stop. That means the stream is left looking like this:

    NL

    On the next read, cin>> skips the leading whitespace(NL = newline), so there is no input, and cin>> hangs. In response, you hit ^Z, and cin reads that in and the loop terminates. Why does that ^Z cause a stream error the second time??

    Maybe with windows you have to set two bits to get the eof error: the first ^Z sets the first bit, and the second ^Z sets the second bit. But, that doesn't explain why you don't get an eof message displayed. At the end of this thread:

    Loop question

    I showed that an eofbit error won't end the while loop. So, after your second ^Z, execution should have entered the while loop and the program should have displayed an eof message. That must mean your last read sets both the eofbit and failbit error flags. I guess that makes sense: since ^Z is not an int, failbit must get set in response to trying to assign ^Z to an int.
    Last edited by 7stud; 05-01-2005 at 04:20 PM.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Answers: istream_iterator

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 01:45 AM
  2. Reading and processing input from keyboard
    By papagaio in forum C Programming
    Replies: 1
    Last Post: 11-12-2008, 02:59 PM
  3. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  4. Input file stream problems
    By Shadow12345 in forum C++ Programming
    Replies: 10
    Last Post: 10-06-2002, 06:33 PM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21