Hi All I'm new to mac and c++ programming.
In the code below I am trying to store the first character of the entered
text. Problem: For some reason it adds extra characters at the end even
though I've only asked for the first character, it adds extra four ?'s at the end:
Example:
What is your name?
Smith
Welcome Smith.
The first letter of your name is S????.
len is 5
Heres the code:
Code:
int main (int argc, char * const argv[]) {
char firstchar [1];
printf("What is your name?\n");
fgets( gname, knameLength, stdin );
if (strlen(gname) > 0)
{
gname[ strlen(gname) -1 ] = '\0';
printf("Welcome %s. \n",gname);
firstchar[0] = '\0';
firstchar[0] = gname[0];
printf("The first letter of your name is %s. \n",firstchar);
printf("len is %d",strlen(firstchar));
}
return 0;
}
Any ideas