So the assignment is:
Write a C++ program to read a name from the keyboard in the form last name, first name, middle name (with last and first names separated by a comma) like so:
Smith, John Henry
Van Brunt, Morris Benjamin
Jones, Mary
And display the name in the form first name, middle name, last name (with no commas) like so:
John Henry Smith
Morris Benjamin Van Brunt
Mary Jones
It needs to be able to loop unless no characters are inputted.
So I had something like this:
Code
//Take a name from Last, First Middle to First Middle Last
#include <iostream>
using namespace std;
int main()
{
cout << "Meant to arrange name from Last name, middle name and first name to first name, middle name and last name. \n \n";
string firstName, middleName, lastName;
while(true)
{
cout << "Please insert name as last name, first name middle name here: " << endl;
firstName = "", middleName = "", lastName = "";
cin >> lastName;
cin >> firstName;
cin >> middleName;
lastName.erase(lastName.end() - 1);
if (lastName == "")
break;
cout << firstName + " ";
if (middleName != " ")
cout << middleName + " ";
cout << lastName << endl << endl;
}
return 0;
}
But my professor said I needed to be using the getline command and substr. And that's where I am lost as I don't know what to do. The problem with my code above is that it won't break if I don't input anything.
e/
When I tried using getline, I had very little luck, so I tried doing this (which I figured would give me wrong results but I thought I'd try anyway)
Code
//CS110 Assignment 02 Rick Gordon
//Take a name from Last, First Middle to First Middle Last
#include <iostream>
using namespace std;
int main()
{
cout << "Rearrange the name to First Middle Last" << endl << endl;
string first;
string middle, last;
while (true)
{
cout << "Please insert name here as last name, first name middle name: " << endl << endl;
getline (cin,last);
getline (cin,first);
getline (cin,middle);
if (last=="") break;
cout<< first << " " << middle <<" "<< last << endl << endl;
}
return 0;
}
This post was edited by Barcelona1011 on Jan 29 2014 11:55am