Thread: isspace()

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    isspace()

    What is wrong with that code?
    It executes else() but wont execute if(). It just hangs.

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main()
    { 
       char x;
       for (;;)
       {
            cout << "Type a character: " << endl; 
            cin >> x;
            if( isspace(x) )
            {
                cout << "It's a space." << endl;
            }
            else
            {
                cout << "It's not a space." << endl;  
            }
        }
    }
    Last edited by Ducky; 03-10-2008 at 01:49 PM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think the problem is that formatted input ignores whitespace by default. Perhaps the simplest option is to just use cin.get() instead.
    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
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah works for me using cin.get(x) instead.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Cool, thank you!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. for and isspace
    By izzy in forum C Programming
    Replies: 2
    Last Post: 03-15-2009, 06:18 PM
  2. pointer with isspace()
    By aze in forum C Programming
    Replies: 6
    Last Post: 04-17-2004, 09:43 PM
  3. isspace() is the way!
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2003, 10:23 AM
  4. Isspace ??? Anyone Know?
    By justin69enoch in forum C Programming
    Replies: 12
    Last Post: 01-23-2003, 05:02 PM