Name Arranger program- error C2228
Aug 23, 2015 at 8:30pm UTC
I am building a name arranger that takes in a full name and sorts it in the order of lastname, firstname middlename. I get an error when trying to pull length of array. (Error C2228 left of '.length' must have class/struct/union)
I have read through the chapter again and still don't seem to understand what the issue is. Thank you for reading
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
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int size =100;
char firstname[size];
char middlename[size];
char lastname[size];
int length = 0;
cout << "Please enter your first name: " ;
cin.getline(firstname, size);
length += firstname.length();
cout << "Please enter your middle name: " ;
cin.getline(middlename, size);
length += middlename.length();
cout << "Please enter your last name: " ;
cin.getline(lastname, size);
length += lastname.length();
char fullname[length +1];
fullname.copy(lastname);
fullname.copy(firstname);
fullname.copy(middlename);
cout << fullname[length];
}
Last edited on Aug 23, 2015 at 8:31pm UTC
Aug 23, 2015 at 8:40pm UTC
firstname is array of characters. It cannot have members, like length(), and in fact arrays in C++ are dumb and do not know hteir size.
You probably meant for firstname to be as std::string.
Topic archived. No new replies allowed.