How do you store two words in one string variable?

Discussion in 'Programmer's Corner' started by onebird, Jun 13, 2010.

  1. onebird

    Thread Starter New Member

    Mar 27, 2010
    I have the following piece of code for Dev C++;

    usimg namespace std;


    cout<<"enter name and surname";

    ... //represents other code


    when a single string is stored in variable names, there is no problem. The problem is that I cant figure out how to store two words separated by a space in a single variable. Can anyone help me out with this?
  2. BMorse

    AAC Fanatic!

    Sep 26, 2009
    onebird likes this.
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    The variable you are using isn't storing the string directly, it is pointing to a region of RAM that is storing the string, hence the '*' pointer symbol.

    Strings in C and C++ are generally stored as NULL terminated, so when something is entered with 'cin', it is written to memory, followed by a NULL. 'cin' typically uses the Carriage Return for the signal to end the string.

    I'm not sure why you aren't getting first and last names, though. You could use a library as mentioned above, or ask for them separately.

    Using a RegEx lib would allow you to pattern match out the space delineated first name and last name, and possibly the middle initial into different variables, but writing that RegEx spans from simple to difficult, depending on how much you want to get and use.

    What does


    give you right after the entry?

    Does it retain the full name/Entry if you put quotes around the input such as "John Smith"? This could be a quirk/feature of your compiler.
  4. onebird

    Thread Starter New Member

    Mar 27, 2010
    Thanks. I think I will stick with using structures for now. Am a bit inexperienced
    still gives me the first name only. I think asking for the names separately is the only option I have. I had wanted to ask for both name and surname at once so as to reduce the total number of questions that I ask the user.
    Last edited: Jun 14, 2010
  5. Papabravo


    Feb 24, 2006
    There is nothing wrong with having two words separated by a space in either C or C++.
    Code ( (Unknown Language)):
    2. char twowords[] = "Two Words" ;
    The other possibility is to use the char *strcat(char *s, char *t) library function. The purpose is to concatenate strings
    Code ( (Unknown Language)):
    2. char two[] = "Two" ;
    3. char space[] = " ";
    4. char words[] = "Words" ;
    6. char buffer[80] ;
    9.     buffer[0] = '\0' ;
    10.     strcat(buffer, two) ;
    11.     strcat(buffer, space) ;
    12.     strcat(buffer, words) ;
    Does that give you some ideas?
  6. someonesdad

    Senior Member

    Jul 7, 2009
    You have flawed code in that you have not allocated memory for the string's contents. Since you're using C++, try to use STL strings if you can. Your programming error is a common C mistake and is one type that has caused lots of the security holes in so many applications over the years (buffer overruns).

    If you insist on using C-type strings, then first create a buffer of adequate size (either declare it as a local variable or create it with malloc).

    Most importantly, you need to read up on how the << operator works; specifically, what happens when it encounters whitespace. Hint: read Stroustrup, 3rd ed., 21.3.2 "Input of Built-In Types". You can also find this out by using a debugger, which you should always be doing on your code when it doesn't work the way you want it to (this almost always means you don't understand how the feature you're using works).

    Finally, even creating a buffer with a fixed size is fundamentally flawed programming when getting input from a stream if you don't input in chunks. What are you going to do when you dimension the space for n characters, but the user types in n+1 characters? Frankly, it's a problem and you typically have to write a fair bit of code to deal with it; for example, what if the input comes from a stream with lots of potential characters, like a file stream or a socket? One classic C way to deal with this is to input a character at a time and reallocate buffers when you run out of space. (Another way is to input more characters with fgets() and keep track of buffer size and number of characters input.) It's a pain unless you're forced to do it; that's why something like an STL string is a better choice if you can use it.

    Ideally, you should be getting bus errors or core dumps, but this is very compiler/environment dependent. Static checking tools (lint) and memory checking tools (Electric Fence) can help find problems like this or at least print a warning.

    Programming C strings like this is kinda like using a powerful shop tool with no safety guards. Powerful work can be done, but sooner or later an accident is going to occur.

    Oh, and when you post your code, cut and paste is better because typing often leads to code that doesn't compile because of typos. Also, use the code, /code construct because it maintains indenting.
  7. Harrington

    New Member

    Dec 19, 2009
    Someonesdad has the right answer but for the purposes of his exorcises I know what they are looking for The recognition of the fact that CIN >> names ; does not read white space characters You can find more notes on this at this address

    For the purpose of this exorcise which is what they want you to do and find out yourself is the use of the following using istringstream

    Code ( (Unknown Language)):
    2. #include <iostream>
    3. #include <sstream>
    4. #include <string>
    5. using namespace std;
    7. // in main of your C ++ file
    9. int main (){
    10. string name;
    11.    getline( cin, name );
    13. // rest of your code for outputting your string with whitespace  characters
    15. }
    Hope this helps
  8. BMorse

    AAC Fanatic!

    Sep 26, 2009

    I know this thread has been on the back burner lately, but I stumbled upon a tutorial that I believe is exactly what the OP may need to look at, or anyone else having the same questions....

    B. Morse