PDA

View Full Version : Storing multiple words into a single string in C++



the_real_fourthdimension
May 30th, 2008, 03:01 AM
Hey All

How do I store multiple words into a single variable in C++? Say I'm writing a program that takes a user's first and last names and stores them both into a single string. How would I do that? I asked someone this question before and seem to remember them saying something about getline or something like that...
Thanks.

LaRoza
May 30th, 2008, 03:10 AM
Hey All

How do I store multiple words into a single variable in C++? Say I'm writing a program that takes a user's first and last names and stores them both into a single string. How would I do that? I asked someone this question before and seem to remember them saying something about getline or something like that...
Thanks.


#include <iostream>
int main(int argc, char** argv)
{
std::string name;
std::cout << "What is your name? \n" << std::endl;
std::getline(std::cin,name);
std::cout << "You are " << name << std::endl;
return 0;
}

dempl_dempl
May 30th, 2008, 03:11 AM
Before we start:

http://www.cplusplus.com/reference/string/getline.html

I use http://www.cplusplus.com/reference/ site all the time when I program.

Here's an example I've copy+pasted from that link




// getline with strings
#include <iostream>
#include <string>
using namespace std;

int main () {
string str;
cout << "Please enter full name: ";
getline (cin,str);
cout << "Thank you, " << str << ".\n";
}


Cheers!

LaRoza: It seems that you were 2 secs faster than me :)

the_real_fourthdimension
May 30th, 2008, 03:18 AM
Thank you both very much! :)

LaRoza
May 30th, 2008, 03:21 AM
Thank you both very much! :)

Mine is better though.

dempl_dempl
May 30th, 2008, 03:26 AM
No, I have a "Thank you " in my code , which makes it more user-friendly :P

LaRoza
May 30th, 2008, 03:38 AM
No, I have a "Thank you " in my code , which makes it more user-friendly :P

I don't take the weasel way and use "using namespace std;", making the code more explicit, more programmer friendly.