#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
int space=0, length=0;
string name;
string last(name, space+1, length);
char response;
do{
cout << "Enter your full name: ";
getline(cin, name); // get their full name, including spaces.
// display last name
cout << "\t" << name << ", your last name is " << last;
getline(cin, last, ',');
cout << "CONTINUE(y/n)? ";
cin >> response;
cin.ignore(80, '\n');
} while(response == 'Y' || response=='y') ;
system ("pause");
return 0;
}
Also did it like this I'm not understanding how to get last name though.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
string name;
string last;
char response;
do{
cout << "Enter your full name: ";
getline(cin, name); // get their full name, including spaces.
// display last name
cout << "\t" << name << ", your last name is " << last << endl;
cout << "CONTINUE(y/n)? ";
cin >> response;
cin.ignore(10, '\n');
} while(response == 'Y' || response=='y') ;
system ("pause");
return 0;
}
1 2 3 4 5 6 7
Enter your full name: Bill Clinton
Bill Clinton, your last name is
CONTINUE(y/n)? y
Enter your full name: Bill Clinton
Bill Clinton, your last name is
CONTINUE(y/n)?
I did it like this and it somewhat works but I have problem with the middle name such as Barrack Hussein Obama. How do I make it so it ignores the middle name and just gives me the last name???
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
int len, length;
string name;
string last="";
char response;
do{
name = "";
last = "";
len = 0;
length = 0;
cout << "Enter your full name: ";
getline(cin, name); // Get their full name, including spaces.
// display last name
len = name.length(); // Get total length of inputted name
for (int x = len; x > 0; x--) // Start from end of input and find first space
{
if (name[x] != ' ')
length++;
else
x = 0; // end loop
}
for (int x = len - length; x < len; x++)// Start where last name begins
last += name[x]; // Assign last to name ending, one letter at a time
cout << "\t" << name << ", your last name is " << last << endl;
cout << "CONTINUE(y/n)? ";
cin >> response;
cin.ignore(20, '\n');
} while (response == 'Y' || response == 'y');
system("pause");
return 0;
}