Well, if you can use C++, you can use getline() and an std::string...You could also try using %s with scanf(), although this can cause a buffer overflow if the user types in too many letters. I *think* %##s will only accept ## characters, but I don't remember exactly.
#include <stdio.h>
#include <string.h>
int main()
{
char name[ 100 ];
int len;
printf( "Please enter your full name> " );
fgets( name, 100, stdin );
len = strlen( name );
if (name[ len - 1 ] == '\n')
name[ len -= 1 ] = '\0';
printf( "Your name, \"%s\", is %d characters long.\n", name, len );
return 0;
}
On line 8 you are comparing pointers to char arrays; you are not comparing strings. Presuming there is a good reason you aren't using std::string, you can compare it with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <cstring>
int main()
{
usingnamespace std;
char sentance[20];
cin.get(sentance,19);
if (strcmp( sentance, "who are you" ) == 0)
cout << "I am you\n";
else
cout << "Error\n";
return 0;
}
However, you are better off just #including <string> and using either:
8if (string( sentance ) == "who are you")
or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
int main()
{
usingnamespace std;
string sentance;
getline( cin, sentance );
if (sentance == "who are you")
cout << "I am you\n";
else
cout << "Error\n";
return 0;
}