Thread: Recognizing Spaces

  1. #1
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Recognizing Spaces

    Hi,
    I need to have my program recognize the space key. For example if I typed in "Hello Everyone", my program reads back "HelloEveryone". This is a simple example of what I am doing:

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    while(1){
    char letter;
    cin>>letter;
    cout<<letter;
    }
    system("PAUSE");
    return 0;
    }

    Thanks

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    It is an example, so it is not what you're really doing? I mean, this example is probably representive, but the should be found in the real code.

    In general, checking if letter is the space is quite easy:

    if (letter == ' ')

  3. #3
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Look into cin.get() and cin.getline. Depending on what you're trying to do one of these should work for you.

    Code:
    int main()
    {
    char myText[256];
    
    cin.getline(myText, 256);
    
    cout << myText << "\n";
    
    return 0;
    }
    Last edited by Invincible; 05-24-2002 at 03:13 PM.
    "The mind, like a parachute, only functions when open."

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Code:
    int main() {
        char s[101];
        cin.getline(s, 100);
        for (int i=0; i < 100; i++) {
            if (s[i] == ' '){
                continue;
            } else if (s[i] == '\0') {
                break;
            } else {
                cout << s[i];
            }
        return 0;
    }

  5. #5
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    char s[101];
    cin.getline(s, 100);
    Should be:

    char s[101];
    cin.getline(s, 101);

    No need to drop the null character cin.getline will take care of that.

    Read the post again. I think this is the old newbie question: Why does cin ignore spaces? He/she is trying to output "Hello Everyone," rather than "HelloEveryone."

    cin.getline will solve this.
    Last edited by Invincible; 05-25-2002 at 01:19 AM.
    "The mind, like a parachute, only functions when open."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  4. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM
  5. Replies: 5
    Last Post: 06-30-2003, 12:52 PM