Hello everyone, I am having a problem with this project at the moment so far i have this. I would be very thankful if someone can help me figure out what i am doing wrong and how i can finish writing this code getting so confused. :S
The assignment is:
You are to write a program to accept a single entry (with spaces) of a person's name in the form of; Mr. John Smith
Then out put the following;
4 letters in the first name (John)
5 letters in the last name (Smith)
The program should ask if you would like to process another student (loop).
int main(int argc, constchar *argv[])
{
constint MAX_NAME_LENGTH = 4;
constint MAX_FAMILY_LENGTH = 5;
string name;
string first;
string last;
size_t firspace = 0;
size_t secspace = 0;
char runagain;
do {
// Assumes 3 entries separated by a space.
// Doesn't check if entries are incomplete or more than one space is entered.
cout << "Enter Title, First and Last Name: ";
getline (cin,name);
firspace=name.find(" ",0);
secspace=name.find(" ",firspace+1);
if ((secspace-1) - (firspace+1) >= MAX_NAME_LENGTH)
first = name.substr(firspace+1, MAX_NAME_LENGTH);
else
first = name.substr(firspace+1, secspace-(firspace+1));
last = name.substr(secspace+1,MAX_FAMILY_LENGTH);
cout << "\n\nFirst name: " << first << "\n";
cout << "Last name: " << last << "\n\n";
do {
cout << "Run again (Y/N)? ";
cin >> runagain;
cin.ignore(); // need this to flush the buffer for the getline().
cin.clear();
runagain = toupper(runagain);
} while ((runagain != 'Y') && (runagain != 'N'));
} while (runagain == 'Y');
//system("pause");
return 0;
}