0

Array Output

I 'm trying to print someone's first initial of a middle name in my program For example: user input-John Henry Holiday program output- Holiday, Henry H. #include <iostream> #include <string> using namespace std; string capitalize(string name); string lastNameFirst(string name[]); int main() { string userName[3]; cout << "First name: "; getline(cin, userName[0]); cout << "Middle name: "; getline(cin, userName[1]); cout << "Last name: "; getline(cin, userName[2]); cout << "Your name is: " << lastNameFirst(userName) << endl; system("pause"); return 0; } string capitalize(string name) { int i = 0; int isSpace = 0; if (!name.empty()) { name[0] = toupper(name[0]); } for (i = 1; i < name.length(); i++) { if (name[i] == ' ') { isSpace = 1; } else { if (isSpace) { name[i] = toupper(name[i]); isSpace = 0; } else { name[i] = tolower(name[i]); } } } return name; } string lastNameFirst(string name[]) { string userName = capitalize(name[2]) + ", " + capitalize(name[0]) + " " + capitalize(name[1]); return userName; }

16th Oct 2018, 2:20 AM
Krista Clark
Krista Clark - avatar
2 Answers
+ 5
A few notes first; ● Explain your problem, it seems you haven't already. ● Does this code run? I tested it in Playground and it didn't ask for input. ● For "John Henry Holiday" isn't it supposedly "Holiday, John H" ?? ● Attach a saved code URL instead of pasting a lengthy code snippet like this; in case you don't know how, please follow these steps: » Create or open the code on SoloLearn editor. Don't forget to save any changes made. » On mobile app, tap on Share button on the top right of editor → Copy to clipboard. Note you'll need to save the code before the Share button can be used. » On web browser, copy code URL from browser's address bar. » Edit the original post, remove the code snippet that's already there. » On mobile app, long press on "Description" text input → Paste. » On web browser, right click on "Description" text input → Paste. Waiting on you ...
16th Oct 2018, 7:26 AM
Ipang
+ 3
//If i understand correctly is this what you want to do? #include <iostream> #include <string> #include <algorithm> using namespace std; string capitalize(string& name); string lastNameFirst(string name[]); int main() { string userName[3]; cout << "First name: "; std::cin >> userName[0]; cout << "Middle name: "; std::cin >> userName[1]; cout << "Last name: "; std::cin >> userName[2]; cout << "Your name is: " << lastNameFirst(userName) << endl; system("pause"); return 0; } string capitalize(string& name) { std::transform(name.begin(), name.end(), name.begin(), ::tolower); name[0] = std::toupper(name[0]); return name; } string lastNameFirst(string name[]) { string userName = capitalize(name[2]) + ", " + capitalize(name[0]) + " " + capitalize(name[1])[0]; return userName; }
16th Oct 2018, 9:13 AM
Tanay
Tanay - avatar