1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
//function prototype
void capitalize(string name, string &first, string &mi, string &last);
int main()
{
char run;
string name, first, mi, last;
do
{
cout << "Enter a name. FIRST MI. LAST" << endl;
getline(cin, name);
cin.clear();
cin.ignore(80, '\n');
capitalize(name, first, mi, last);
cout << "You entered: " << name << endl;
cout << "It was converted to: " << last << ", " << first << " " << mi << endl;
cout << "Do you have another name to enter? Y/N" << endl;
cin >> run;
while (toupper(run) != 'Y' && toupper(run) != 'N')
{
cout << "Entry invalid. Do you have another name to enter? Y for yes or N for no." << endl;
cin >> run;
}
} while (toupper(run) == 'Y');
return 0;
}
//function
void capitalize(string name, string &first, string &mi, string &last)
{
int num, count;
char letter;
for (count = 1; count < 4; count++)
{
num = name.find(" ", 0);
if (count == 1)
{
first = name.substr(0, num);
}
else if (count == 2)
{
mi = name.substr(0, num);
}
else if (count == 3)
{
last = name.substr(0, num);
}
else
{
cout << "Error" << endl;
}
name.erase(0, num + 1);
}
first[0] = toupper(first[0]);
mi[0] = toupper(mi[0]);
last[0] = toupper(last[0]);
}
|