+ 1

Is it possible to create a string array? Like say wholeName[3] which would contain first middle and last? If so how?

2nd Feb 2017, 11:40 PM
Tim Dunn
Tim Dunn - avatar
11 ответов
+ 3
#include <iostream> #include <string> int main() { std::string first, middle, last; std::cin >> first; std::cin >> middle; std::cin >> last; std::string wholeName[] = {first, middle, last}; for (int i = 0; i < 3; i++) { std::cout << wholeName[i] + " "; } return 0; }
3rd Feb 2017, 12:02 AM
Kawaii
Kawaii - avatar
+ 3
Remove the {first,miiddle,last} from 'string wholeName[3]{first,miiddle,last};' and also remove all the endl in the cin, change the ':' after the second cout to an ';' and add ';}' after the return (if it's not already there and you justforgot to copy it over) and it should work.
3rd Feb 2017, 12:13 AM
Robobrine
Robobrine - avatar
+ 2
string myArray[3] = {"First", "middle", "last"};
2nd Feb 2017, 11:52 PM
Kawaii
Kawaii - avatar
+ 2
It works like any other array: string name[] = {"Monkey", "D.", "Luffy"}; cout << name[0] << " " << name[1] << " " << name[2] << endl;
2nd Feb 2017, 11:52 PM
Robobrine
Robobrine - avatar
+ 1
What error are you getting? It should work without initializing the strings directly: string name[3]; cin >> name[0] >> name[1] >> name[2]; cout << name[0] << " " << name[1] << " " << name[2] << endl;
3rd Feb 2017, 12:02 AM
Robobrine
Robobrine - avatar
0
I want the user to define these themselves I'm getting an error because I'm not including {} after my definition of the string wholeName[3] if I included place holders like firstName Thay will rewrite with cin << I'm assuming OK thanks
2nd Feb 2017, 11:58 PM
Tim Dunn
Tim Dunn - avatar
0
#include <iostream> using namespace std; int main() { string wholeName[3]{first,miiddle,last}; cout << "Whats your first name? "; cin >> wholeName[0] endl; cout << "Whats your middle name? ": cin >> wholeName[1] endl; cout << "Whats your last name? "; cin >> wholeName[2]; endl; cout << wholeName[0] << " " << wholeName[1] << " " << wholeName[2]; return 0 I'm still getting an error in c4droid
3rd Feb 2017, 12:08 AM
Tim Dunn
Tim Dunn - avatar
0
#include <iostream> #include <string> using namespace std; int main() { string wholeName[3] cout << "Whats your first name? "; cin >> wholeName[0] endl; cout << "Whats your middle name? "; cin >> wholeName[1] endl; cout << "Whats your last name? "; cin >> wholeName[2]; endl; cout << wholeName[0] << " " << wholeName[1] << " " << wholeName[2]; return 0; } OK I made the changes it didn't affect the error I'm getting it says aggregate 'std::_ndk1string name has incomplete data type and cannot be defined and it points to line 7 oh my code
3rd Feb 2017, 12:24 AM
Tim Dunn
Tim Dunn - avatar
0
You still have all the endl in your cins and you deleted the ';' after string wholeName[3]
3rd Feb 2017, 12:30 AM
Robobrine
Robobrine - avatar
0
OK I will not run in c4droid but ran just fine in code playground Humm that's really frustrating.
3rd Feb 2017, 12:32 AM
Tim Dunn
Tim Dunn - avatar
0
#include <iostream> #include <string> using namespace std; int main() { string wholeN[3]; cout << "Whats your first name? "; cin >> wholeN[0]; cout << "\nWhats your middle name? "; cin >> wholeN[1]; cout << "\nWhats your last name? "; cin >> wholeN[2]; cout << endl; cout << wholeN[0] << " " << wholeN[1] << " " << wholeN[2]; return 0; } OK this code runs great on code playground thanks for the help
3rd Feb 2017, 12:51 AM
Tim Dunn
Tim Dunn - avatar