How do you store two words in one string variable?

Thread Starter

onebird

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

include<iostream>
usimg namespace std;

main()
{

char*names;
cout<<"enter name and surname";
cin>>names;

... //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?
 

thatoneguy

Joined Feb 19, 2009
6,359
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

cout<<*names;

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.
 

Thread Starter

onebird

Joined Mar 27, 2010
16
Thanks. I think I will stick with using structures for now. Am a bit inexperienced
and
cout<<*names;
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:

Papabravo

Joined Feb 24, 2006
21,225
There is nothing wrong with having two words separated by a space in either C or C++.
Rich (BB code):
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
Rich (BB code):
char two[] = "Two" ;
char space[] = " ";
char words[] = "Words" ;

char buffer[80] ;


    buffer[0] = '\0' ;
    strcat(buffer, two) ;
    strcat(buffer, space) ;
    strcat(buffer, words) ;
Does that give you some ideas?
 

someonesdad

Joined Jul 7, 2009
1,583
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.
 

Harrington

Joined Dec 19, 2009
85
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
http://www.fredosaurus.com/notes-cpp/io/reading-chars.html

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

Rich (BB code):
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

// in main of your C ++ file 

int main (){ 
string name;
   getline( cin, name );

// rest of your code for outputting your string with whitespace  characters

}
Hope this helps
 

BMorse

Joined Sep 26, 2009
2,675
I have the following piece of code for Dev C++;

include<iostream>
usimg namespace std;

main()
{

char*names;
cout<<"enter name and surname";
cin>>names;

... //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?

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....

http://www.daniweb.com/tutorials/tutorial71858.html

B. Morse
 
Top