Very basic question

Is there something similar to string that can hold multiple words? for example I want to define someone's name and last name as a single character in the code.
I want to define someone's name and last name as a single character

You can't store a first name and last name as a single character. I assume you meant as a single element.

You can use std::pair to store two strings.
1
2
3
4
#include <utility> 
#include <string>

std::pair<std::string,std::string> fred ("Flintstone", "Fred");


Or more commonly, just create a struct that contains both names.
1
2
3
4
5
6
7
#include <string>

struct person_name
{  std::string last_name;
    std::string first_name;
};
person_name fred = {"Flintstone", "Fred"};

Yeah I have trouble with... using the proper terms in english :D That is what I meant tho, thank you...

The struct method seems cleaner to me, and how should I go about asking the user to enter his full name while having to click enter only once?
Last edited on
Yeah I have trouble with... using the proper terms in english

No problem. I was able to figure out what you meant.

how should I go about asking the user to enter his full name while having to click enter only once?

cin works for this.
1
2
3
4
5
6
7
8
  person_name person;

  if (cin >> person.first_name >> person.last_name)
  {  //   Two names entered
  }
  else
  {  // problem with cin
  }

cin will separate the two names based on a space between the names. Works fine for the simple case of two names. It's a problem if the the first or last name has two parts though.

Last edited on
Thanks a lot man ^^
Topic archived. No new replies allowed.