just wondering if anyone could help me with this problem I have, the program always crashes for some reason and I'm not sure why. I'm still new to C++ so, yeah.
What it's supposed to do:
- Create an array of strings with 5 possible elements.
- Get 5 names from the user (first name and last name)
- Print the first and last elements of the array of strings
- Create a string and have user assign a sentence to the string.
- Print the string from (4)
Steps 4,5 were easy but I really don't get how to display the last element of the string in step 3.
int x;
string n1,n2,n3,n4,n5,user;
string array[5]={n1,n2,n3,n4,n5};
cout<<"Enter 5 names: "<<endl;
getline(cin,n1);
getline(cin,n2);
getline(cin,n3);
getline(cin,n4);
getline(cin,n5);
cout<<endl;
for(x=0;x<5;x++)
{
int y=array[x].length();
cout<<array[x].substr(0,1)<<"\t"<<array[x].substr(y,1)<<endl;
}
cout<<endl;
cout<<"Enter sentence for string: "<<endl;
getline(cin,user);
cout<<user<<endl;
system("pause");
return 0;