CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Posts
    2

    Breaking out of loop

    Hi everyone, I have C++ primer 5th edition and doing a couple of examples in chapter three. The code has a while loop and I know how to break out of it by using ctrl z, but the code in the book doesn't use it. Is there something I am not doing or did they probably assume the reader would know? here is the code from the book.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    using std::vector;
    
    int main()
    {
    	string word;
    
    	vector<string> text;
    
    	while(cin >> word)
    	{
    		text.push_back(word);
    	}
    
    	for(int i = 0; i < text.size(); i++)
    		cout << text[i] << endl;
    
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Breaking out of loop

    cin is a c++ file stream. A file stream has 4 possible states - good (available for use), eof (end of file has been reached), fail (logical error on i/o operation) and bad (read/write error on i/o operation).

    Simplistically, when using stream extraction, if the state of the stream is good, then it returns a reference to the stream (which is non-zero). If stream is in one of the other states, stream extraction returns NULL (0). Hence the while condition is satisified as long as the stream is in the good state.

    If you get a bad state, then something is wrong as this usually means that an expected i/o operation has gone wrong (often due to hardware problems). If the state is fail, then this usually means that you are trying to extract a type incompatible with the data available (ie trying to extract an int when the data is abc). If the state is eof, then the end of data has been reached. This is most often used when reading from files as opposed to the keyboard as for cin.

    So to terminate the while loop, the state of cin has to changed from good to one of the other states. As you are are extracting a type string, really the only non-good state available is eof - as string will accept any character so you can't 'induce' a fail state (if you were reading a set of say integers, then you could terminate the loop by entering a non-numeric value). To induce an eof state from the keyboard, you use ctrl z as you state in your post.

    To terminate a loop like this without inducing an eof, another way is use a special 'input sequence' (or sentinel value http://en.wikipedia.org/wiki/Sentinel_value) to terminate the loop. In this case you could check to see if the extracted string was, say, 'endofloop' and if it was equal to this value then terminate the loop.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2013
    Posts
    2

    Re: Breaking out of loop

    Quote Originally Posted by 2kaud View Post
    cin is a c++ file stream. A file stream has 4 possible states - good (available for use), eof (end of file has been reached), fail (logical error on i/o operation) and bad (read/write error on i/o operation).

    Simplistically, when using stream extraction, if the state of the stream is good, then it returns a reference to the stream (which is non-zero). If stream is in one of the other states, stream extraction returns NULL (0). Hence the while condition is satisified as long as the stream is in the good state.

    If you get a bad state, then something is wrong as this usually means that an expected i/o operation has gone wrong (often due to hardware problems). If the state is fail, then this usually means that you are trying to extract a type incompatible with the data available (ie trying to extract an int when the data is abc). If the state is eof, then the end of data has been reached. This is most often used when reading from files as opposed to the keyboard as for cin.

    So to terminate the while loop, the state of cin has to changed from good to one of the other states. As you are are extracting a type string, really the only non-good state available is eof - as string will accept any character so you can't 'induce' a fail state (if you were reading a set of say integers, then you could terminate the loop by entering a non-numeric value). To induce an eof state from the keyboard, you use ctrl z as you state in your post.

    To terminate a loop like this without inducing an eof, another way is use a special 'input sequence' (or sentinel value http://en.wikipedia.org/wiki/Sentinel_value) to terminate the loop. In this case you could check to see if the extracted string was, say, 'endofloop' and if it was equal to this value then terminate the loop.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured