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



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

除了输入分数的计算平均值外,一切都很好。

我似乎不知道该怎么做,尽管它应该很简单,但我就是无法理解

我目前得到的错误是:

"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 Archivesnn");
printf("1. Display Students' Detailsn");
printf("2. Calculate average of all students’ marks n");
printf("3. Add new student to the record n");
printf("4. Quit Programn");
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 againn");
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(" %sn", 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' namen");
scanf(" %[^n]s", temp->name);
printf("Enter the students' IDn");
scanf("%d", &temp->id);
printf("Enter the students' markn");
scanf("%f", &temp->mark);
fprintf(fptr, "%s %d %.2f", temp->name, temp->id, temp->mark);
count++;
}
fclose(fptr);
free(temp);
free(temp->name);
}

发布的代码不编译!

在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+");
struct student *temp = (struct student *)malloc(sizeof(struct student));
if (fptr == NULL)
printf("nError.");

总是在调用C库函数后立即检查错误指示。

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

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

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

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输入,因此stdin中的下一个字符不可能是s。当调用任何scanf()函数族时,请始终检查返回值以确保操作成功。当使用输入格式说明符CCD_ 16和/或CCD_。建议删除格式字符串中尾随的s,检查返回值并限制可以输入的字符总数,如:

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

关于:

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

不存在阵列CCD_ 19,因此这将永远不会产生期望的结果。

关于错误消息:

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

当然,不能将"float"值添加到指向char数组的指针中。你到底想完成什么?

以上只是发布代码中问题的冰山一角。建议使用调试器并逐步通过您的代码来确定的许多问题

最新更新