define "space" as a character

This is a discussion on define "space" as a character within the C++ Programming forums, part of the General Programming Boards category; how do we define "space"as a character such that when there is a space between 2 words, it is taken ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Unhappy define "space" as a character

    how do we define "space"as a character such that when there is a space between 2 words, it is taken as a character ???

    thanks
    godfather

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well, standard cin eats whitespace, if you want to get whitespace, either use cin.getline to grab an entire input string, or cin.get to get a character regardless of type.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Depends what you are talking about... space on input... space on output...

    if you had a space in a file...
    Code:
    ...
    
    ifstream ins;
    char c[100];
    int i=0;
    
    ...
    while ( (!ins.eof()) && (i<100) )
    {
       ins.get(c[i]);
       ++i
    }
    
    ...
    ...Your spaces would be counted as characters and loaded into the character array.

    What are you trying to do exactly?
    Blue

  4. #4
    Unregistered
    Guest
    use ifstream::operator<<() then ifstream::get() back and forth.

    ie:
    Code:
    while (fin.good()) {
      fin << word;//read everything till whitespace (' ','\n','\t')
      if (fin.get() == ' ') cout << "Space\n";//read only the next whitespace (beware there could be more)
    }
    to deal with more than one whitespace character, instead of doing that fin.get(), do this:
    Code:
    do
      fin.get();
    while (fin.peek() == ' ' || fin.peek() == '\n' || fin.peek() == '\t');
    just some simple work around to using getline() for the entire line and get() for each character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implenting Function Keys
    By frassunit in forum C Programming
    Replies: 14
    Last Post: 11-12-2008, 10:31 AM
  2. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 05:11 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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