#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//Variables
string first_name;
string last_name;
cout << "Please enter your first and last name: ";
getline (cin, first_name, last_name);
cout << "Hello, " << first_name << last_name << ".";
}
This fails at the getline, only working successfully if only one variable is used. I'm very new to C++ and learning with several books. One suggests it'll work using cin >> variable1 >> variable2 but I was hoping to avoid using cin >>. So, can getline store more than one variable on a single prompt? If so what's the format?
@LB my reasoning that I posted it that way is only because it's a lot less confusing on the eyes for newer programmers to see if that way versus all on one line.
Mary Magdalene, getline is better in a sense that if you're dealing with names that may have more than one word you'll be sure to get all of the info. Some people's names have multiple spaces in them
You should never worry about efficiency. Always use the most elegant practical solution, and worry about optimization only after you have profiled your working code and found the bottlenecks.