Enter the user’s input into separate variables using cin

I have to write a program that prompts the user for their first name, then prompts the user for their last name, and then returns a greeting.

What is your first name?
George
What is your last name?
Washington
Hello George Washington.

I tried

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystr;
cout << "What's your first name? ";
getline (cin, mystr);
cout << "What is your last name";
getline (cin, mystr);

And my cout now has to greet both names and i can't figure how to do that....


Thanks in advance

You need two separate variables, as your title states
Why not make both a first, and last name variable?

1
2
3
4
5
6
7
8
9
10
11
int main()
{
     string firstName;
     string lastName;
     
     cout << "What's your first name?"
     cin >> firstName;

     cout << "What's your last name?"
     cin >> lastName;
}
I know but I am confused how to create two separate variables ?
I started c++ two days ago and I am learning it in english which is my 2nd language since I'm an international student in the states....it will take some time till everything will make sense.

Two variables ?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystr1;
string mystr2;
cout << "What's your first name? ";
getline (cin, mystr1);
cout << "What is your last name";
getline (cin, mystr2);

?? or ?
Yes, that is exactly right.
Success !!!

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystr1;
string mystr2;
cout << "What's your first name? ";
getline (cin, mystr1);
cout << "What is your last name? ";
getline (cin, mystr2);
cout << "Hello " << mystr1 << mystr2 << "\n";
return 0;
}


I managed to do it, I have one more question tough:

How do i get a space between mystr1 and mystr2 because when the greet pops it says the two names without a space between them.

Thank you.
Yes, that's correct.

A variable is like a cup. You can fill it with water, but if you were to want to put coca-cola in the cup, you'd first need to empty the water.

Originally, you were using one variable: string mystr;
Then you called getline() to fill your variable with some data (filling the cup with water)
you then called getline() again to fill your variable with different data (emptying the water and putting in coca-cola)

When you tried to output the first and last names with that string, all you were getting was the last name, because you had over-written the first.

The solution being to declare two string variables, each for their own purpose. - two cups, one for water, one for coca-cola.

Hoped that helped you understand.

EDIT:
Didn't see your post.
To put space in between them, simply manipulate your cout call.
Since it appears you know you can add numerous "<<" operators, try putting something between mystr1, and mystr2.
Last edited on
That's very kind of you, thanks for the nice explanation. I understand now .
Topic archived. No new replies allowed.