+ 1

[C++ Help] I'm trying to conversion by using vector and string but when loop back it error

Expected: First Middle Last name -> Last, First M-initial. james herBErt BonD > Bond, James H. Troubleshooting: People without middle name? They only input 2 strings, First name, Last name > Enter. Code: #include <iostream> #include <string> #include <new> using namespace std; // ====================================================================== // Function: capitalize // Purpose: Return str capitalized, non-letters are ignored and kept the same // Examples: abc -> Abc, aBC -> Abc, a. -> A. // Parameters: // str - input string // ====================================================================== void capitalize(string& str); int main() { int NAME_COUNT = 3; //FIRST MIDDLE LAST int i; string word; string* p; char answer = ' '; do { p = new (nothrow) string[NAME_COUNT]; if (p == nullptr) cout << "Error: memory could not be allocated"; else { cout << "Enter first name, middle name, last name? "; cin >> p[0] >> p[1] >> p[2]; //SOMETIME PEOPLE DON'T HAVE MIDDLE NAME //IF p[2] EMPTY >> ???? swap(p[0], p[2]); //swap value swap(p[1], p[2]); //swap value p[0] = p[0] + ","; //add ',' p[2].erase(p[2].begin() + 1, p[2].end()); //conver Initial p[2] = p[2] + "."; //add Ini. cout << "You have entered: "; for (i = 0; i < NAME_COUNT; i++) { capitalize(p[i]); cout << p[i] << " "; } delete[] p; cout << "Wanna do again? (Y/N) "; cin >> answer; } } while (answer != 'N' && answer != 'n'); return 0; cout << "Exit. End of the program." << endl; } void capitalize(string& str) { if (str.length() == 0) { return; } str[0] = toupper(str[0]); for (int i = 1; i < str.length(); i++) { str[i] = tolower(str[i]); } }

27th Oct 2019, 4:04 AM
Phuc Hoang Trinh
Phuc Hoang Trinh - avatar
3 Réponses
30th Oct 2019, 3:24 AM
Phuc Hoang Trinh
Phuc Hoang Trinh - avatar
0
im using array, string now
30th Oct 2019, 3:25 AM
Phuc Hoang Trinh
Phuc Hoang Trinh - avatar