isspace()

This is a discussion on isspace() within the C++ Programming forums, part of the General Programming Boards category; What is wrong with that code? It executes else() but wont execute if(). It just hangs. Code: #include <iostream> #include ...

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

    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    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
    856
    Cool, thank you!
    Compiler MSVC++ 2013 with Code::Blocks.

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. Purpose of isspace??
    By aznprincess888 in forum C Programming
    Replies: 15
    Last Post: 01-31-2008, 10:24 PM
  3. pointer with isspace()
    By aze in forum C Programming
    Replies: 6
    Last Post: 04-17-2004, 09:43 PM
  4. isspace() is the way!
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2003, 09:23 AM
  5. Isspace ??? Anyone Know?
    By justin69enoch in forum C Programming
    Replies: 12
    Last Post: 01-23-2003, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21