Thread: why my program doesn't read the end of file sequence

  1. #1
    Registered User
    Join Date
    Aug 2014
    Location
    Italy
    Posts
    5

    why my program doesn't read the end of file sequence

    i have a question, i'm studyng the string library type and i have to write this program :

    Code:
    std::string word; 
    while (std::cin >> word) {
    std::cout << word << std::endl; 
    
    }

    why if my input is :

    hi my name is ^Z

    the output is :

    hi
    my
    name
    is

    why the program doesn't fall out from the while loop ?
    why my program does not recognize my sequence of end of file ?

    i'm a beginner and i need help...thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Enter the control sequence represented by ^Z on a separate line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    On windows for emulating EOF condition - it could be Ctrl^D not Ctrl^Z
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by vart View Post
    On windows for emulating EOF condition - it could be Ctrl^D not Ctrl^Z
    Under windows, no it's not. CTRL-D is unix, not windows.

    The usual requirement under windows when reading from stdin without redirection (e.g. from a file), is that EOF is represented by a newline followed by CTRL-Z, as laserlight described.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Aug 2014
    Location
    Italy
    Posts
    5
    Quote Originally Posted by grumpy View Post
    Under windows, no it's not. CTRL-D is unix, not windows.

    The usual requirement under windows when reading from stdin without redirection (e.g. from a file), is that EOF is represented by a newline followed by CTRL-Z, as laserlight described.
    i can't understand.
    why in this program the end of file sequence works :

    Code:
    #include <iostream> 
    #include <string> 
    
    
    int main()
    {
        int val = 0, currVal = 0; 
    
        if (std::cin >> val) {
            int cnt = 1; 
    
            while (std::cin >> currVal) {
    
                if (val == currVal)
                    ++cnt; 
    
                else {
    
                    std::cout << val << " occurs " << cnt << " times " << std::endl; 
                    val = currVal; 
                    cnt = 1; 
                }//end of else 
            }//end of while 
    
            std::cout << val << " occurs " << cnt << " times " << std::endl;
            system("pause"); 
            
        }//end of outhermost if
    
        return 0; 
    
    }
    here in this program the end of file sequence works perfectly but with the type string it doesn't, why ?
    thank you

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up what "if (std::cin)" and "while (std::cin)" are actually testing ..... it is not end of file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Do I Read a File to a Program?
    By KittenAqua in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2012, 07:05 PM
  2. Why Won't My Program Read My .hpp file?
    By Insidia in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2012, 11:59 AM
  3. Basic file read, file write program
    By starwarsyeah in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 03:23 PM
  4. Replies: 5
    Last Post: 10-08-2004, 08:12 AM

Tags for this Thread