CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    5

    how to hit enter to end the loop

    if i wrote something like while(cin>>n){...}
    how do i hit enter to let the loop end?

  2. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to hit enter to end the loop

    This is a bit of a convoluted solution, but there we go:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
      std::string str;
      int n(0);
    
      while(std::getline(std::cin, str) && !str.empty())
      {
        std::stringstream ss(str);
        ss >> n;
    
        std::cout << "N: " << n << std::endl;
      }
    
      return 0;
    }
    Last edited by PredicateNormative; March 23rd, 2010 at 06:27 AM. Reason: Fixed incorrect code. Removed needless <limits> header

  3. #3
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to hit enter to end the loop

    By the way, do you want it to quit the loop if you use non-numerical characters? If so, you could write:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
      std::string str;
      int n(0);
    
      while(std::getline(std::cin, str) && !str.empty() && (std::stringstream(str) >> n))
      {
        std::cout << "N: " << n << std::endl;
      }
    
      return 0;
    }

  4. #4
    Join Date
    Mar 2010
    Posts
    5

    Re: how to hit enter to end the loop

    gee, i don't really know enough to understand your reply
    but thanks anyway

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to hit enter to end the loop

    Quote Originally Posted by franfyh View Post
    gee, i don't really know enough to understand your reply
    but thanks anyway
    I'll explain then Consider this line:

    Code:
    while(std::getline(std::cin, str) && !str.empty() && (std::stringstream(str) >> n))
    And now lets break down the conditions:
    Code:
    std::getline(std::cin, str)
    This takes the whole line inputted to cin, and writes it into str which is an std::string. The result of std::getline can be implicitly cast to a bool, the bool will be true if the getline operation was successful, else it will be false.

    If the getline is succcessful, then the runtime will move onto the next condition in the while loop (else it will abort the while loop if the getline result is false). If there is something in the line then str will not be empty, and therefore
    Code:
    !str.empty()
    will be true ( the '!' character means 'not').

    If the above statement is true, then the runtime will move on to the next condition (else it will abort the while loop if the string is empty - i.e. the above condition is false).

    The last statement is a little more complicated as there are two operations going on:

    Code:
    (std::stringstream(str) >> n)
    The first is creating a temporary stringstream object:
    Code:
    std::stringstream(str)
    The above line copies the data from the string str, into a temporary stringstream object that has no name. That stringstream object can then be used in the same way as std::cin to convert the stream data that it holds into the integer n. That is what the >>n part is doing. If the operation of copying the string data into a stringstream, and then the operation of converting the stringstream data into an integer is successful, then the result of
    Code:
    (std::stringstream(str) >> n)
    will be true, else false. If it is false, then the likelyhood is that the data held in the string is not a number and therefore the while loop will quit.

    I hope this helps.
    Last edited by PredicateNormative; March 24th, 2010 at 06:50 AM.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: how to hit enter to end the loop

    Here is an idea. Change it to
    Code:
    while(!(cin >> n))
    What is n? An Int? Well anyway when you are ready to end the loop just input something that is not an integral value (if n is an int) and the loop will end.

    If you want a better solution post a more complete example of what you are doing and perhaps someone can design something even better.

    Another obvious solution is too simply break out of the loop when n is a certain value. Even in that case you should still design the program such that it can detect input failures and differentiate between an input failure and an input indicating that the loop should end. The idea comes from here. I think that the OP should read sections 15.1-15.7.
    http://www.parashift.com/c++-faq-lit....html#faq-15.3
    Last edited by kempofighter; March 25th, 2010 at 11:29 AM. Reason: added link

  7. #7
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: how to hit enter to end the loop

    Quote Originally Posted by kempofighter View Post
    Here is an idea. Change it to
    Code:
    while(!(cin >> n))
    What is n? An Int? Well anyway when you are ready to end the loop just input something that is not an integral value (if n is an int) and the loop will end.

    If you want a better solution post a more complete example of what you are doing and perhaps someone can design something even better.

    Another obvious solution is too simply break out of the loop when n is a certain value. Even in that case you should still design the program such that it can detect input failures and differentiate between an input failure and an input indicating that the loop should end.
    I don't think you've thought that through. With your solution, if
    Code:
    cin >> n
    is successful, the code will break out of the while loop because your '!' will make the result of the condition false.

    Your second solution of breaking out of the while loop if n is a certain value will not work either, because if n is an integer, then the body of the while loop will not be entered if cin simply gets a return value.

  8. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: how to hit enter to end the loop

    Quote Originally Posted by PredicateNormative View Post
    I don't think you've thought that through. With your solution, if
    Code:
    cin >> n
    is successful, the code will break out of the while loop because your '!' will make the result of the condition false.

    Your second solution of breaking out of the while loop if n is a certain value will not work either, because if n is an integer, then the body of the while loop will not be entered if cin simply gets a return value.

    I thought it through very well actually and it might be correct depending on what the OP is trying to do (which neither of us knows). The purpose of my while loop was to loop until valid data is entered. If there is some other criteria that must be met then that would also have to be part of the loop or there may need to be multiple loops. Since we have no idea what the OP is trying to do (he hasn't posted a complete example or set of requirements), I can't really suggest a complete solution and neither can anyone else without taking a wild guess.

    I assumed that the OP was trying to loop until good data was entered but I don't know for sure. The loop he posted was nonsensical in the first place.

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