Thread: Ending a loop, by pushing SPACE ?

  1. #1
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62

    Question Ending a loop, by pushing SPACE ?

    How do you end the loop, by pushing SPACE ?
    I've tried using the value 32.... doesn't work.


    Code:
    	while ( value == 32 || value < 0 )
        {
    		std::cin >> value;
    
    		if ( value < 0 )
    		{
    		sum += 1;
    		}
        }

    regards,


    The SharK
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You cannot do it like that because operator >> ignores all whitespace. You can use get() to get a single character at a time if you are getting characters. If you are getting integers, how were you expecting the integers to be separated in the first place? Normally they are separated by whitespace.

    If you are expecting each value to be separated by a newline, and then a space ends the loop, then there is no simple solution. I'd suggest using getline to read in a string, then converting each string to an int with a stringstream. Before doing that check for an empty string which means the user typed enter without inputting a number. You should also check the result of the stringstream conversion to make sure the value entered really was a number.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    while ( value == 32 || value < 0 )
    This ends when you enter a non-space character, you got it reversed...
    And as Daved said, you can't get it working like this.
    Last edited by maxorator; 11-25-2006 at 01:50 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    yeah, maxorator

    ya, know when you program late at night....

    actually I end up writing this instead of using SPACE:

    Code:
    	while ( value != 0 )
        {
    		std::cin >> value;
    
    		if ( value < 0 )
    		sum += 1;
        }
    Studying programming languages,
    you'll ALWAYS be a student ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM