user-0 提问: 新人请教C语言，用结构体写的程序

回复:
user: user-1
content: 这个问题我无法解释，但只要删掉scanf里的','就一切正常了 #include <stdio.h>  struct student { 	int num; 	char name[10]; 	int score[3]; };  void input(struct student p[],int n) { 	int i; 	for(i=0;i<n;i++) 	{ 		scanf("%d%s%d%d%d",&p[i].num,p[i].name,&p[i].score[0],&p[i].score[1],&p[i].score[2]); 	} } void print(struct student m[],int n) { 	int i; 	printf("\n"); 	for(i=0;i<n;i++) 	{ 		printf("%7d,%4s,%4d,%4d,%4d\n",m[i].num,m[i].name,m[i].score[0],m[i].score[1],m[i].score[2]); 	} }  int main() { 	struct student s[2]; 	input(s,2); 	print(s,2); 	return 0; }

user: user-1
content: 引用 3 楼 user-5 的回复:scanf("%d%s%d%d%d",&p[i].num,p[i].name,&p[i].score[0],&p[i].score[1],&p[i].score[2]); scanf输入格式里面是不需要其他不需要的符号的这个可以有

user: user-2
content: scanf, wscanf Read formatted data from the standard input stream.  int scanf( const char *format [,argument]... );  int wscanf( const wchar_t *format [,argument]... );  Routine Required Header Compatibility  scanf <stdio.h> ANSI, Win 95, Win NT  wscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  Both scanf and wscanf return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.  Parameters  format  Format control string  argument  Optional arguments  Remarks  The scanf function reads data from the standard input stream stdin and writes the data into the location given by argument. Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. If copying takes place between strings that overlap, the behavior is undefined.  wscanf is a wide-character version of scanf; the format argument to wscanf is a wide-character string. wscanf and scanf behave identically otherwise.   Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tscanf scanf scanf wscanf    For more information, see Format Specification Fields — scanf functions and wscanf Functions.   Example   /* SCANF.C: This program uses the scanf and wscanf functions   * to read formatted input.   */  #include <stdio.h>  void main( void ) {    int   i, result;    float fp;    char  c, s[81];    wchar_t wc, ws[81];     printf( "\n\nEnter an int, a float, two chars and two strings\n");     result = scanf( "%d %f %c %C %s %S", &i, &fp, &c, &wc, s, ws );    printf( "\nThe number of fields input is %d\n", result );    printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws);     wprintf( L"\n\nEnter an int, a float, two chars and two strings\n");     result = wscanf( L"%d %f %hc %lc %S %ls", &i, &fp, &c, &wc, s, ws );    wprintf( L"\nThe number of fields input is %d\n", result );    wprintf( L"The contents are: %d %f %C %c %hs %s\n", i, fp, c, wc, s, ws); }   Output  Enter an int, a float, two chars and two strings 71 98.6 h z Byte characters  The number of fields input is 6 The contents are: 71 98.599998 h z Byte characters  Enter an int, a float, two chars and two strings 36 92.3 y n Wide characters  The number of fields input is 6 The contents are: 456 92.300003 y n Wide characters   Floating-Point Support Routines, Stream I/O Routines, Locale Routines  See Also   fscanf, printf, sprintf, sscanf

user: user-3
content: 写多几个scanf.或者像赵老师说的检查scanf的返回值，看看scanf录入了多少个变量。

user: user-0
content: 引用 4 楼 benny89415 的回复:断点测试下就知道了。。。 你的录入char【10】的时候直接把“，”也录入进去了，要知道，“，”也是char的一种。明白了，谢谢

user: user-0
content: 引用 3 楼 user-5 的回复:scanf("%d%s%d%d%d",&p[i].num,p[i].name,&p[i].score[0],&p[i].score[1],&p[i].score[2]); scanf输入格式里面是不需要其他不需要的符号的 哪些是其他的符号呢？不好意思，有点不太明白

user: user-4
content: 引用 3 楼 user-5 的回复:scanf("%d%s%d%d%d",&p[i].num,p[i].name,&p[i].score[0],&p[i].score[1],&p[i].score[2]); scanf输入格式里面是不需要其他不需要的符号的 符号是可以作为判定的，但是，录入字符串的时候就不好判定了

user: user-4
content: 断点测试下就知道了。。。 你的录入char【10】的时候直接把“，”也录入进去了，要知道，“，”也是char的一种。

user: user-5
content: scanf("%d%s%d%d%d",&p[i].num,p[i].name,&p[i].score[0],&p[i].score[1],&p[i].score[2]); scanf输入格式里面是不需要其他不需要的符号的

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:请检查scanf的返回值！ 可以说的详细点吗，scanf有返回值吗

user: user-2
content: 请检查scanf的返回值！