deleting a space from a string

This is a discussion on deleting a space from a string within the C++ Programming forums, part of the General Programming Boards category; say I have this code. Code: #include <iostream.h> #include <conio.h> #include <cstring.h> main() { string input; cin>>input; } im writing ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    72

    deleting a space from a string

    say I have this code.

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <cstring.h>
    
    main()
    {
    
        string input;
    
        cin>>input;
    }
    im writing to a database so if someone inputs a space, it will get screwed up.

    This is what i people usually will input:

    SMITH,JOHN << lastname comma firstname

    If they input:

    SMITH, JOHN << lastname comma space firstname

    The question is, can i delete the space from the input, so i have only the first example. All code i do needs to be in cstring.h. Any examples is appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try this instead of the call to cin's >> operator:

    getline ( cin, input );

    -Prelude
    My best code is written with the delete key.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Just a few corrections.

    Code:
    #include <iostream.h> //iostream, no .h
    #include <conio.h>
    #include <cstring.h> //cstring, not .h
    
    using namespace std;
    
    main() //eek! use int main( ). main( ) is c.
    {
    
        string input;
    
        cin>>input;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    someguy
    Guest
    #include <cstring.h> //cstring, not .h

    actually if you take off the .h, you need to add an additional c at the begging as follows:

    #include <ccstring>

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Really?

    Why is that? I thought that it was <cstring> for c++ and <string.h> for c.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM

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