Is there a way to check a string for a space

This is a discussion on Is there a way to check a string for a space within the C++ Programming forums, part of the General Programming Boards category; If you read my other thread you know that I am making a character creation program to assist my cousins ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Post Is there a way to check a string for a space

    If you read my other thread you know that I am making a character creation program to assist my cousins program. 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 (All the cause of a infinite loop as far as i can tell). Here is the PORTION of code that is causing the error.
    Really I need to know if there is a way to check input for spaces to prevent incorrect input.
    NOTE that this is just part of my program if you want the entire thing just ask and I can send the .cpp and accompaning files.
    Code:
    switch ( a[2] ) {
    					   case 1:
    							cout <<"Ok, This Is The Easiest Step. Simply Type In The Name You Want.\n";
    							cout <<"Type Name: (NO SPACES)";
    							cin >>name[1];
    							cin.ignore();
    							cout.flush();
    							cout <<"You Entered // "<< name[1] <<" // as your name.\n";
    							cout <<"If your name is incorrect just go to character menu again and change it.\n";
    							cout <<"Press ENTER to return to character menu\n";
    							z[2] = 1;
    							cin.get();
    							break;
    Well if you see my problem and see a possible solution Hopefully you share it. THANK YOU ALL IN ADVANCE.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Code:
    // cin.getline(<variable>, <size of buffer>, <delimeter>);
    // ex.
    
    cin.getline(string1, 20, '\n');
    Also, remember that cin.ingore() we talked about in the last post? This is where it matters.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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 10:04 PM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Oh no I just wanted name requirements to be like most other games. NO spaces but it can contain other characters like ()[]!@#$%^&*,.<>.I just didnt want the user to use spaces which is why I didnt use getline. And for another cin.getline uses the memory for the buffer whether you input that much or not. string only use memory for the amount of characters in the string. If anyone has a more efficient or less program threating way of doing it then let me know. I really dont understand how get line works anyways. Everytime I use it, the first character of the input is removed when I try to output it to screen to verify correct input.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I really dont understand how get line works anyways. Everytime I use it, the first character of the input is removed when I try to output it to screen to verify correct input.
    What happens when you run this:
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    
    	string input = "";
    	
    	cout<<"Enter a sentence:\n";
    	getline(cin, input);
    
    	cout<<endl<<"You entered:\n"<<input<<endl;
    	
    	
    	return 0;
    }
    Really I need to know if there is a way to check input for spaces to prevent incorrect input.
    You can't read in a space with the operator>>, so in order to check if the user input more than one word, you have to use getline(). Read what the user entered into a variable of type string with getline(), and then use the string function find() to look for the space character: ' '. If you find a space, make the user enter another name(a do-while loop would prove useful).
    Last edited by 7stud; 11-24-2005 at 03:14 AM.

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, 03: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

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