C 从数组和分离结构计算平均值

C 从数组和分离结构计算平均值,c,C,试图制作一个程序来打印输入到文件中的数据 除了计算所输入分数的平均值外,一切正常 我似乎不知道该怎么做,尽管它应该很简单,但我就是想不起来 我当前遇到的错误是: "temp->mark = temp->mark + studentArray[j];" (Invlalid operands to binary + (have 'float' and 'char *'). 如果有人能帮助我,我将不胜感激。我试过以下方法 #include <stdio.h> #inclu

试图制作一个程序来打印输入到文件中的数据

除了计算所输入分数的平均值外,一切正常

我似乎不知道该怎么做,尽管它应该很简单,但我就是想不起来

我当前遇到的错误是:

"temp->mark = temp->mark + studentArray[j];" (Invlalid operands to 
 binary + (have 'float' and 'char *').
如果有人能帮助我,我将不胜感激。我试过以下方法

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct student{
    char name[30];
    int id;
    float mark;
};

int count = 0;
void student_update(char *stuadd);
void display(char *stuadd);

void main(int argc, char *studentArray[100])
{
    int choice;
    while(1)
    {
        printf("Welcome to Student Archives\n\n");
        printf("1. Display Students' Details\n");
        printf("2. Calculate average of all students’ marks \n");
        printf("3. Add new student to the record \n");
        printf("4. Quit Program\n");
        scanf("%d",&choice);

        switch(choice)
        {
        case 1:display(studentArray[100]);
            break;
        case 2:
            break;
        case 3:
            student_update(studentArray[100]);
            break;
        case 4: printf("Program Terminated.\n"); 
            exit(0);
        default: printf("Wrong Choice. Enter again\n");
            break;
        }
    }
}

void display(char *stuadd)
{
    FILE *fptr;
    char ch;
    int rec = count;
    fptr = fopen("stuadd.txt", "r");
    struct student *temp = (struct student *)malloc(sizeof(struct student));
    if (fptr == NULL)
        printf("File does not exist.");
    else
    {
        while (rec)
        {            
            fread(temp->name, 50, 1, fptr);
            printf(" %s\n", temp->name);
            fread(&temp->id, sizeof(int), 1, fptr);
            printf("%d", temp->id);
            fread(&temp->mark, sizeof(int), 1, fptr);
            printf("%.2f", temp->mark);

            rec--;
        }
    }
    fclose(fptr);
    free(temp);
    free(temp->name);
}

void calculateAverage(char *studentArray[100])
{
    struct student *temp = (struct student *)malloc(sizeof(struct student));
    int j;
    float avg;

    temp->mark  = avg = 0;

    for(j = 0; j < 100; j++)
    {
        temp->mark = temp->mark + studentArray[j];
    }

    avg = (float)temp->mark / j;

    printf("Average of students' total marks are: %.2f",avg);
}

void student_update(char *stuadd)
{
    FILE *fptr;
    fptr = fopen("stuadd.txt", "a+");
    struct student *temp = (struct student *)malloc(sizeof(struct student));

    if (fptr == NULL)
        printf("\nError.");
    else
    {
        printf("\nEnter the students' name\n");
        scanf(" %[^\n]s", temp->name);

        printf("Enter the students' ID\n");
        scanf("%d", &temp->id);

        printf("Enter the students' mark\n");
        scanf("%f", &temp->mark);

        fprintf(fptr, "%s %d %.2f", temp->name, temp->id, temp->mark);

        count++;
    }

    fclose(fptr);
    free(temp);
    free(temp->name);
}
#包括
#包括
#包括
#包括
结构学生{
字符名[30];
int-id;
浮标;
};
整数计数=0;
无效学生_更新(char*studd);
无效显示(字符*添加);
void main(int argc,char*studentArray[100])
{
智力选择;
而(1)
{
printf(“欢迎访问学生档案”\n\n);
printf(“1.显示学生的详细信息\n”);
printf(“2.计算所有学生的平均分数\n”);
printf(“3.将新学生添加到记录\n”);
printf(“4.退出程序\n”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:显示(studentArray[100]);
打破
案例2:
打破
案例3:
student_更新(studentArray[100]);
打破
案例4:printf(“程序终止。\n”);
出口(0);
默认值:printf(“选择错误。请重新输入\n”);
打破
}
}
}
无效显示(字符*添加)
{
文件*fptr;
char ch;
int rec=计数;
fptr=fopen(“stuadd.txt”,“r”);
struct student*temp=(struct student*)malloc(sizeof(struct student));
如果(fptr==NULL)
printf(“文件不存在”);
其他的
{
while(rec)
{            
fread(临时->名称,50,1,fptr);
printf(“%s\n”,临时->名称);
fread(&temp->id,sizeof(int),1,fptr);
printf(“%d”,temp->id);
fread(&temp->标记,尺寸(int),1,fptr);
printf(“%.2f”,温度->标记);
记录--;
}
}
fclose(fptr);
免费(临时);
免费(临时->名称);
}
无效计算权限(字符*学生阵列[100])
{
struct student*temp=(struct student*)malloc(sizeof(struct student));
int j;
浮动平均值;
温度->标记=平均值=0;
对于(j=0;j<100;j++)
{
临时->标记=临时->标记+学生阵列[j];
}
平均=(浮动)温度->标记/j;
printf(“学生总分的平均值为:%.2f”,平均值);
}
无效学生_更新(字符*添加)
{
文件*fptr;
fptr=fopen(“stuadd.txt”,“a+”);
struct student*temp=(struct student*)malloc(sizeof(struct student));
如果(fptr==NULL)
printf(“\n错误”);
其他的
{
printf(“\n输入学生的姓名”);
scanf(“%[^\n]s”,临时->名称);
printf(“输入学生ID\n”);
scanf(“%d”,&temp->id);
printf(“输入学生分数”);
scanf(“%f”,&temp->mark);
fprintf(fptr,“%s%d%.2f”,temp->name,temp->id,temp->mark);
计数++;
}
fclose(fptr);
免费(临时);
免费(临时->名称);
}

发布的代码无法编译

在ubuntu linux下,使用:

gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" (in directory: /home/richard/Documents/forum)
编译器输出以下内容:

untitled.c:16:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(int argc, char *studentArray[100])
      ^~~~

untitled.c: In function ‘main’:
untitled.c:16:15: warning: unused parameter ‘argc’ [-Wunused-parameter]
 void main(int argc, char *studentArray[100])
               ^~~~

untitled.c: In function ‘display’:
untitled.c:48:10: warning: unused variable ‘ch’ [-Wunused-variable]
     char ch;
          ^~

untitled.c:45:20: warning: unused parameter ‘stuadd’ [-Wunused-parameter]
 void display(char *stuadd)
                    ^~~~~~

untitled.c: In function ‘calculateAverage’:
untitled.c:83:33: error: invalid operands to binary + (have ‘float’ and ‘char *’)
         temp->mark = temp->mark + studentArray[j];
                      ~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~

untitled.c:86:29: warning: conversion to ‘float’ from ‘int’ may alter its value [-Wconversion]
     avg = (float)temp->mark / j;
                             ^

untitled.c: In function ‘student_update’:
untitled.c:91:27: warning: unused parameter ‘stuadd’ [-Wunused-parameter]
 void student_update(char *stuadd)
                           ^~~~~~

Compilation failed.
还有一些其他问题,如:

free(temp);
free(temp->name);
这是指在分配的内存被传递到
free()
之后,将指针访问分配的内存。结果是不确定的行为。建议:

free(temp->name);
free(temp);
FILE *fptr;
fptr = fopen("stuadd.txt", "a+");

if ( !fptr )
{
    perror("fopen failed");
    exit( EXIT_FAILURE );
}

// implied else, fopen successful

struct student *temp = malloc(sizeof(struct student));
if( !temp )
{
    perror( "malloc failed" );
    exit( EXIT_FAILURE );
}

// implied else, malloc successful
关于下列声明

FILE *fptr;
fptr = fopen("stuadd.txt", "a+");
struct student *temp = (struct student *)malloc(sizeof(struct student));

if (fptr == NULL)
    printf("\nError.");
始终在调用C库函数后立即检查错误指示

将错误消息输出到
stderr
,而不是
stdout

当错误指示来自C库函数时,立即调用
peror()
输出您的错误消息和系统认为发生错误的文本原因,全部转到
stderr

调用任何堆分配函数时:
malloc
calloc
realloc
,1)返回的类型为
void*
,可以分配给任何指针。强制转换只会使代码变得混乱,使其更难理解、调试等。2)始终检查(!=NULL)返回值以确保操作成功。建议:

free(temp->name);
free(temp);
FILE *fptr;
fptr = fopen("stuadd.txt", "a+");

if ( !fptr )
{
    perror("fopen failed");
    exit( EXIT_FAILURE );
}

// implied else, fopen successful

struct student *temp = malloc(sizeof(struct student));
if( !temp )
{
    perror( "malloc failed" );
    exit( EXIT_FAILURE );
}

// implied else, malloc successful
关于:

scanf(" %[^\n]s", temp->name);
对scanf()的调用是毫无意义的。由于输入格式说明符
%[^\n]
在遇到换行符序列时将停止从
stdin
输入,因此在调用任何
scanf()
函数族时,
stdin
中的下一个字符不可能成为
s
,始终检查返回值以确保操作成功。使用输入格式说明符
%s
和/或
%[…]
时,始终包含一个小于输入缓冲区长度的最大字符修饰符,以避免缓冲区溢出的任何可能性(以及由此产生的未定义行为)。建议删除格式字符串中的尾随
s
,检查返回值并限制可输入的字符总数,如:

 if( scanf(" %29[^\n]", temp->name) != 1 )
 {
     fprintf( stderr, "scanf failed to input the student name\n" );
     exit( EXIT_FAILURE );
 }
关于:

for(j = 0; j < 100; j++)
{
    temp->mark = temp->mark + studentArray[j];
}
当然,“float”值不能添加到指向char数组的指针中。你到底想完成什么


以上所述只是发布代码中问题的“冰山一角”。建议使用调试器并逐步检查代码,以确定发布的代码无法编译的许多问题

在ubuntu linux下,使用:

gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" (in directory: /home/richard/Documents/forum)
编译器输出以下内容:

untitled.c:16:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(int argc, char *studentArray[100])
      ^~~~

untitled.c: In function ‘main’:
untitled.c:16:15: warning: unused parameter ‘argc’ [-Wunused-parameter]
 void main(int argc, char *studentArray[100])
               ^~~~

untitled.c: In function ‘display’:
untitled.c:48:10: warning: unused variable ‘ch’ [-Wunused-variable]
     char ch;
          ^~

untitled.c:45:20: warning: unused parameter ‘stuadd’ [-Wunused-parameter]
 void display(char *stuadd)
                    ^~~~~~

untitled.c: In function ‘calculateAverage’:
untitled.c:83:33: error: invalid operands to binary + (have ‘float’ and ‘char *’)
         temp->mark = temp->mark + studentArray[j];
                      ~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~

untitled.c:86:29: warning: conversion to ‘float’ from ‘int’ may alter its value [-Wconversion]
     avg = (float)temp->mark / j;
                             ^

untitled.c: In function ‘student_update’:
untitled.c:91:27: warning: unused parameter ‘stuadd’ [-Wunused-parameter]
 void student_update(char *stuadd)
                           ^~~~~~

Compilation failed.
还有一些其他问题,如:

free(temp);
free(temp->name);
这是指在分配的内存被传递到
free()
之后,将指针访问分配的内存。结果是不确定的行为。建议:

free(temp->name);
free(temp);
FILE *fptr;
fptr = fopen("stuadd.txt", "a+");

if ( !fptr )
{
    perror("fopen failed");
    exit( EXIT_FAILURE );
}

// implied else, fopen successful

struct student *temp = malloc(sizeof(struct student));
if( !temp )
{
    perror( "malloc failed" );
    exit( EXIT_FAILURE );
}

// implied else, malloc successful
关于下列声明

FILE *fptr;
fptr = fopen("stuadd.txt", "a+");
struct student *temp = (struct student *)malloc(sizeof(struct student));

if (fptr == NULL)
    printf("\nError.");
始终在调用C库函数后立即检查错误指示

将错误消息输出到
stderr
,而不是
stdout

当错误指示来自C库f时