Thread: Is there a way to check a string for a space

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Now I ran into a problem of if when asking user to input preferred name. If they put a space the porgram either uses allof my memory or crashes OR locks up my computer
    Is there a reason you want to limit the user to entering one word names? Variables of type string (#include <string>) do not have a limit on the size of the string they can contain. Here is an example:
    Code:
    string str = "something";
    cout<<str<<endl;
    
    str = "Something a little longer.";
    cout<<str<<endl;
    You can use a different form of getline() than SlyMaelstrom posted to read data into a string type:
    Code:
    getline(source, str);
    'source' can be 'cin' or a file. getline() will read in data until it encounters a '\n'. Or, if you need to read in multiple lines into one variable, you can specify a character to signal when to stop reading as the 3rd paramter of getline() e.g.:
    Code:
    getline(source, str, '#');
    That will keep reading in data until a '#' character is encountered in the input source.
    Last edited by 7stud; 11-23-2005 at 11:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM