我目前正在编写一些代码,我知道它有问题。我的代码提示用户输入名称,并使用fgets () or sscanf ()
. 如果用户输入错误(即数字或字母数字大小写),它应该打印错误消息并再次要求输入,直到用户输入正确的输入。我也初始化了:
char name [47];
printf ( "Name: " );
//some code dealing with newline character with the use of fgets
if ( (sscanf (name, %s, name)) == 1 )
//some code dealing with this condition
else {
do {
printf ( "ERROR: Invalid name. Name should consist of letters only.\n" );
printf ( "Name: " );
if (fgets ( name, sizeof (name), stdin ) == '\0' )
//some code dealing with EOF
} while ((sscanf (name, %s, name)) != 1);
}
谁能告诉我怎么了?