Thread: strip spaces

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    strip spaces

    well i have a problem here.
    when i enter names with leading/trailing spaces they get stored as it is.
    how to remove leading/trailing spaces from the strings entered?
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
      vector<string> name_list;
    
      cout << "Enter names or '$' to stop:" << endl;
      while (true)
      {
        string tmp;
    // i though this would help, but not. i still get leading spaces.
        cin.setf(ios::skipws);
        getline(cin, tmp, '\n');
        if (tmp == "$") break;
        name_list.push_back(tmp);
      }
    
      sort(name_list.begin(), name_list.end());
    
      for (int i=0; i<name_list.size(); i++)
      {
        cout << name_list[i] << endl;
      }
        
    
      return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Boost has trim functions, but you could write your own easily. Find first non-white-space character and erase up to it. Then find the last non-white-space character and remove everything following it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, getline kindof reads the entire line the user enters and does not stop (or trim) if it encounters spaces or such.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    getline() is raw. It does not understand what skipws means. It just reads bytes until end of line. Don't use getline() for this task.

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