Sign in to follow this  
uncle_rico

Resolved: Using std::cin as the condition of a while loop

Recommended Posts

Hey guys. Let me first say that I'm a big fan of C++ Primer from Addison Wesley. After reading the What is a good C++ book/tutorial? thread, I realized that all of my C++ books were crap and were probably the source of a lot of confusion, which in turn is the source of my hack-ass code style. So, even though I've mastered C++ enough to program my own games in SDL, it's obvious that I am still severely lacking as a C++ programmer, and so I decided to pick up this beginners book and re-train myself. I am very happy that I did, because I've learned so much already, even in the very first chapter. In my opinion, the book might be a bit too confusing for total newcomers, as it dives right into functions and classes without fully explaining what they are, but because I am not a total newcomer I can't say for sure. Anyway, one of these new things I learned is that you can place a std::cin statement as the condition of a while loop, and it will keep checking for input until none is provided, at which point the loop will cease to execute. I wonder, how does this work in practice? I programmed something to test this concept, based on one of the programming examples in the book. I was expecting the overloaded >> operator to somehow parse my input, such that it would interpret spaces as delimeters between separate input strings. So, I could therefore enter one string (like "2 3 4 5 6") and the >> operator would read it as 5 separate inputs ("2", "3", "4", "5", and "6") and the while loop would execute once for each input. However, I was not quite sure that this was the case, so I at first only put in one value. When I did that, it gave me another input prompt as if it was asking for another one! So I thought to myself, "Ok, it's going to ask for values one at a time, and it will keep asking me for input until I hit return without giving it a value." In retrospect, this makes a lot more sense, because if I enter a value when it prompts me, the value that I enter becomes the result of the std::cin >> value statement, which causes the statements after the while loop to execute, after which the program loops back to the while condition, where I'm asked for a new input. So, I put in a few more values, hitting Enter after each one, and then when I decided it was enough, I left the next prompt blank (no value) and just hit Enter. But, it didn't work. It keeps giving me the input prompt as if the while loop is still executing. So, I definitely don't understand how this works! Again, I am using C++. I am running this program from the Windows command prompt, and it was compiled with the MSVC++ Toolkit using Code::Blocks as my IDE. Here is the source code (in case this behavior is a result of a bug):
#include <iostream>
#include <Sales_item.h>

int main()
{

 Sales_item total, trans;

 if(std::cin >> total)
 {

  while(std::cin >> trans)
  {

   if(trans.same_isbn(total))
   {

    total = total + trans;

   }
   else
   {

     std::cout << total << std::endl;
     total = trans;

   }

  }
  std::cout << total << std::endl;

 }
 else
 {

  std::cout << "No input!" << std::endl;
  return -1;

 }

 return 0;

}



[Edited by - uncle_rico on April 22, 2006 12:44:56 PM]

Share this post


Link to post
Share on other sites
You have to enter EOF (control + z). the result of cin will be true if cin is in a good state i.e not EOF or bad input. Therefore the only way to break that loop is to enter an unexpected value or enter EOF which is generally ctrl + z.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this