I'm having trouble my program to display whitespace between each namestring in the fullName array without the whitespace being included the string length.
#include <iostream>
#include <cstring>
usingnamespace std;
int main(void)
{
autochar myFirst[50];
autochar myMiddle[50];
autochar myLast[50];
autochar myFull[50];
cout << "Please enter your first name: ";
cin.getline(myFirst, 50);
cout << "Please enter your middle name: ";
cin.getline(myMiddle, 50);
cout << "Please enter you last name: ";
cin.getline(myLast, 50);
cout << endl;
strcpy(myFull, myFirst);
strcat(myFull, myMiddle);
strcat(myFull, myLast);
cout << "Your full name is: " << myFull << endl;;
cout << "The total characters in your name is " << strlen(myFull) << endl;
if (0 == strcmp(myFirst, myMiddle))
{
cout << "Your first and middle names are the same." << endl;
}
if (0 == strcmp(myFirst, myLast))
{
cout << "Your first and last names are the same." << endl;
}
if (0 == strcmp(myMiddle, myLast))
{
cout << "Your middle and last names are the same." << endl;
}
return 0;
}
The question is why you would even want to do that in the first place. What do you think std::string is for? Besides, with strcat the destination string must be large enough to hold both, it's original content as well as the string to be appended. If you just left that whole c stuff out you wouldn't have such problems.
My main problem is getting the strlen function to count the concatenated string "myFull" without the spaces. For instance, if the full name was "John William Smith" the strlen would count it as 18 instead of 16 due to the whitespaces.