Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么这个程序的结果总是零?_C - Fatal编程技术网

C 为什么这个程序的结果总是零?

C 为什么这个程序的结果总是零?,c,C,我正试图写一个简单的程序,接收最多30名学生的分数、姓名和ID,并打印他们的平均分数。我想知道为什么程序的结果总是零。 如果有人能给我一个提示,我将不胜感激。 代码如下: #包括 结构学生{ 字符名[30]; 整数标记; int-ID; }s[30]; int main(){ int n,i=0,和=0; 浮动平均; /*printf(“输入学生人数:”)*/ scanf(“%d”和“&n”); /*printf(“输入他们的信息:”)*/ 对于(i=0;i格式说明符%s说读取字符串,它正在这样

我正试图写一个简单的程序,接收最多30名学生的分数、姓名和ID,并打印他们的平均分数。我想知道为什么程序的结果总是零。
如果有人能给我一个提示,我将不胜感激。
代码如下:

#包括
结构学生{
字符名[30];
整数标记;
int-ID;
}s[30];
int main(){
int n,i=0,和=0;
浮动平均;
/*printf(“输入学生人数:”)*/
scanf(“%d”和“&n”);
/*printf(“输入他们的信息:”)*/

对于(i=0;i格式说明符
%s
说读取字符串,它正在这样做…包括逗号和后续字符。它是“贪婪的”

然后,
%d
就没有什么可吸收的了,所以所有的整数都保持为零

我建议你一次读一行,然后分析这些行

另外,也有一些建议


当遇到这样的问题时,您应该尝试通过检查调试器中变量的值来缩小其范围,甚至只使用
printf
,如下所示:

    printf("%s|%d|%d\n", s[i].name, s[i].mark, s[i].ID);
当我将其放入第一个循环中,并将以下输入传递给程序:

2
A、 2,3
B、 4,5
…:

A,2,3 | 0 | 0
B、 4,5 | 0 | 0
0

…这清楚地表明解析是问题所在。

问题在这一行:

scanf("%s,%d,%d",s[i].name,&s[i].mark,&s[i].ID);
它不会像你想的那样工作。为了测试它,我修改了你的程序

int main() {
    int n = 10, i=0, sum=0;
    int scanfResult;
    float average;
    /* printf("enter the number of students: "); */
    //scanf("%d", &n);
    /* printf("enter their information: "); */
    for(i=0; i<n; i++)
    {
        scanfResult = scanf("%s,%d,%d",s[i].name,&s[i].mark,&s[i].ID);
        printf("scanf has scanned %d items\n", scanfResult);
        if(scanfResult != 3) 
        {
            printf("you need to find another way of entering the data\n");
            exit(1);
        }
    }
    for(i=0; i<n; i++)
         sum+=s[i].mark; 
    average=sum/(float)n ;
    printf("%.2f", average);
}
intmain(){
int n=10,i=0,和=0;
int scanfResult;
浮动平均;
/*printf(“输入学生人数:”)*/
//scanf(“%d”和“&n”);
/*printf(“输入他们的信息:”)*/

对于(i=0;i您的问题是
扫描中(“%s,%d,%d”,s[i].name,&s[i].mark,&s[i].ID);

我将代码更改如下:

scanf("%s%d%d",s[i].name,&s[i].mark,&s[i].ID);
然后输入和输出为: 输入:

输出:

1.50

如果您试图输入带有空格的名称,如

Joe Smith,90,1234
然后问题是,
%s
转换说明符在第一个空白字符处停止读取,因此它读取
“Joe”
,并留下
“Smith”
在输入流中,这会妨碍接下来的两次读取。或者,如果没有任何空格,
%s
将整个输入行读取到
名称中(无论是否合适,都可能导致缓冲区溢出)

您应该使用
%[
转换说明符而不是
%s
,并显式调整其大小,以避免缓冲区溢出的风险。您还应该检查
scanf
的返回值,以确保正确读取所有3项:

/**
 * loop until we have three good inputs;
 * %29[^,] reads up to 29 characters or to the next comma;
 * An array of size 30 can store a string of up to
 * 29 characters plus the string terminator;
 * Added leading blank in the format string to consume
 * any leading whitespace. 
 */
while ( scanf( " 29%[^,],%d,%d", s[i].name, &s[i].mark, &s[i].ID) != 3 )
{
   fprintf(stderr, "Bad input, try again\n" );
   /**
    * clear out the input stream before trying again
    */
   while ( getchar() != '\n' )
     ; // empty loop body
}

你知道C不关心你的缩进,是吗?这几乎就像让缩进成为一件有意义的事情是一个愚蠢的想法,肯定没有任何语言会使用:)不管怎样,你是否尝试过验证个人的
s[i]。标记
值是什么?你能展示一下你输入值的方式吗?我已经说过了!
/**
 * loop until we have three good inputs;
 * %29[^,] reads up to 29 characters or to the next comma;
 * An array of size 30 can store a string of up to
 * 29 characters plus the string terminator;
 * Added leading blank in the format string to consume
 * any leading whitespace. 
 */
while ( scanf( " 29%[^,],%d,%d", s[i].name, &s[i].mark, &s[i].ID) != 3 )
{
   fprintf(stderr, "Bad input, try again\n" );
   /**
    * clear out the input stream before trying again
    */
   while ( getchar() != '\n' )
     ; // empty loop body
}