C++ newbie here. Writing a simple program. Everything works,except when I attempt to extract firstname and surname and print these individually, the number of letters printed in surname will always be the same size as the number in firstname. So if the name is will jackson, the program will print firstname as: will and second name as: jack. How do I solve this?
here is the code
for( i = 0; i < 19; i++)
if(cAddress[i] == ' ' || cAddress[i] == '_' || cAddress[i] == '-' || cAddress[i] == '.')
break;
strncpy(cFirst, cAddress, i);
cFirst[i] = '\0';
cout << endl << "\n";
cout << "Your first name is " << cFirst << endl;
strcpy(cSur,cAddress + i + 1);
cSur[i] = '\0';
cout << endl;
cout << "Your surname is " << cSur << endl;
You are adding a
\0
character at the (i+1)th position - this marks the end of string, so that's all it prints. Before that line, cSur probably contains the entire surname.