In: Computer Science
C++ Question
4. Formulate the following conditions
• A last name starts with the letter H.
• A last name starts with the letters Mac.
• A last name comes before "Jones".
I have done this program to best of your requirement as mentioned by you.
If anything more is needed leave comments.
#include <iostream>
using namespace std;
int main(){
char choice = 'y';
string previousLastName = "";
do{
string firstName;
string lastName;
cout << "Enter First Name: ";
cin >> firstName;
cout << "Enter Last Name: ";
cin >> lastName;
// 1. A last name starts with the letter 'H'
if( lastName[0] == 'H')
cout << "Last Name ["<< lastName <<"] starts with the letter H.\n";
// 2. A last name starts with "Mac"
if ( lastName[0] == 'M' )
if ( lastName[1] == 'a' )
if ( lastName[2] == 'c' )
cout << "\nLast Name ["<< lastName <<"] starts with the letters ""Mac \n";
// 3. A Last Name comes before "Jones"
if ( 0 == lastName.compare("Jones") ){
if ( 0 == previousLastName.compare(""))
cout << "\nThere was no Last Name before 'Jones' \n";
else
cout << "\nLast Name ["<< previousLastName <<"] comes before 'Jones'.\n";
}
cout << "Do you want to enter again ? y/n : ";
cin >> choice;
previousLastName = lastName;
}while(choice == 'y' || choice == 'Y');
return 0;
}