So I am trying to write a program that reads in a first middle and last name and then returns in the name in the form: Last, First MiddleInitial. (with the period) for example: Input: Sarah Anne Jane would be Jane, Sarah A.
The program also needs to account for those inputs that do not give a middle name. I am completely lost as to how to configure my program to detect if there is a middle name or not. Also, I am just reprinting the name whereas I need to be printing the last name first. Any help would be great!!!!
#include <iostream>
#include <string>
using namespace std;
string nextString(string list, int index);
int split(string names, string array[], int n);
int main()
{
char ans;
int value = 30, count;
string first, last, name, full, ray[value];
do
{
//Obtain the full name
cout << "Please Enter Your First Middle and Last name" << endl;
getline (cin, full);
//Send the string to the splitting function
count = split(full, ray, value);
// Print the new ray
for(int i=0; i<count; i++)
cout << ray[i] << endl;
//Complete the do while loop
cout << "Would you like to continue?" << endl;
cin >> ans;
} while ((ans == 'y') || (ans == 'Y'));
return 0;
}
//Function to find the next index where a new word starts
string nextString(string list, int index)
{
return list.substr(index,list.find('\0',index)-index);
}
//Function to split the names inside the string
int split(string names, string array[], int n)
{
int i=0, start=0;
do
{
array[i]=nextString(names, start);
i++;
start=names.find('\0', start)+1;
} while (start!=0);
#include <iostream>
#include <string>
#include <algorithm>
usingnamespace std;
int main()
{
string fullName = "";
cout << "Please enter your full name: " << flush;
getline(cin, fullName);
int numSpace = count(fullName.begin(), fullName.end(), ' ');
if (numSpace == 1)
cout << "You did not enter a middle name " << endl;
elseif (numSpace == 2)
cout << "You did enter a middle name " << endl;
cin.ignore();
return 0;
}
alternatively you could use a vector and a stringstream
I understand what you did with the first program with counting spaces, I am just lost as to the printing it out in the of last, first beginning middle initial. Thank you for the help on counting spaces!!