尝试编写一个程序来打印输入到文件中的数据。
除了输入分数的计算平均值外,一切正常。
我似乎不知道该怎么做,尽管它应该很简单,但我就是无法理解它。
我目前得到的错误是:
"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);
}
最佳答案
发布的代码无法编译!
在 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();
输出您的错误消息和系统认为发生错误的文本原因,全部到 标准错误
当调用任何堆分配函数时:malloc
calloc
realloc
, 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()
系列函数时,始终检查返回值以确保操作成功。当使用输入格式说明符 %s
和/或 %[...]
时,总是包含一个比输入缓冲区的长度少一个的 MAX CHARACTERS 修饰符,以避免缓冲区溢出的任何可能性(以及由此产生的未定义行为)。建议去掉格式字符串尾部的,并检查返回值,限制可以输入的字符总数,如:
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];
}
没有数组 studentArray[]
所以这永远不会产生预期的结果。
关于错误信息:
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 数组的指针。您实际上想要完成什么?
以上只是发布代码中问题的“冰山一角”。建议使用调试器并单步执行代码以确定许多问题
关于从数组和单独的结构计算平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682143/