Hey All,
This is a program to extract the middle name from a person's full name, but it only works for a person who has a first, middle and a last name. What do I do so that it works for a person who has just a first and last name? For example: Fabio or Santa Claus. I am not supposed to use conditional statements, other wise using if...else it would have been so easy. I can't think of a way to do it without if...else. Any ideas/suggestions would be great. Thanks a lot, Farah.
Here's my program:
/* ----------------------------------------------------------------------
Program: Program 6.cpp
Purpose: This program extracts the middle name from a person's full name
Written: Farah Rasheed
Date : 10/12/06
Version: .NET
------------------------------------------------------------------------*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main(void)
{
// Purpose of the program
cout << "The program reads your full name extracts the middle name." << endl << endl;
// Read in the full name
cout << "Enter your full name: ";
cin.ignore();
string name;
getline(cin, name, '\n');
int lastBlankPos = name.rfind(" ");
int nextToLastBlankPos = name.find(" ", 0);
int length_of_middle_name = lastBlankPos-nextToLastBlankPos;
//cout << lastBlankPos << "\t" << nextToLastBlankPos << "\t" << length_of_middle_name<<"\n";
string middle_name = name.substr(nextToLastBlankPos, length_of_middle_name);
cout << middle_name << endl << endl;
// End of the program
return 0;
}