c++ - 结构[对象] c语言
问题描述
输出:
a) 项目清单
b)给定学习年份的对象列表
c) 给定学院的对象列表
d) 给定大教堂中的对象列表
e) 最大小时数
实现提供菜单操作的功能..
包含字段的对象结构:姓名、年份、教师、大教堂、小时数。
我收到错误:
89 [Error] incompatible types in assignment of 'char [1]' to 'char [4]'
91 [Error] invalid conversion from 'char*' to 'int' [-fpermissive]
92 [Error] invalid conversion from 'char*' to 'int' [-fpermissive]
这是我的代码:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct object{
char title[1];
int year,kol;
char faculty[2];
char cathedra[3];
} t1;
void input(FILE *);
void print(FILE *);
void app(FILE *);
void desh(FILE *);
main(){
char c;
FILE *tf;
while (1){
puts(" 1 Input data");
puts(" 2 Output the number of hours");
puts(" 3 Exit");
c=getch();
switch(c)
{
case '1': input(tf); break;
case '2': desh(tf); break;
case '3': return 0;
default : puts(" Wrong mode");
}
}
}
void input(FILE *tf)
{
char ch;
printf("\n Input data\n");
do
{
printf("\n The name of the object: "); scanf("%s",t1.title);
printf("\n Year of study: "); scanf("%f",t1.year);
printf("\n Name of the faculty: "); scanf("%s",&t1.faculty);
printf("\n Name of the cathedra: "); scanf("%s",&t1.cathedra);
printf("\n Number of hours: "); scanf("%d",&t1.kol);
fwrite(&t1,sizeof(t1),1,tf);
printf("\n Finish input? y/n ");
ch=getch();
}
while (ch != 'y');
fclose(tf);
}
void print(FILE *tf){
int i;
i=1;
fread(&t1,sizeof(t1),1,tf);
while (!feof(tf))
{
printf("\n The name of the object: %s, Year of study: %f, Name of the faculty: %s, Name of the cathedra: %s, Number of hours:%d", i,t1.title,t1.year,t1.faculty,t1.cathedra,t1.kol);
fread(&t1,sizeof(t1),1,tf);
i++;
}
}
void app(FILE *tf)
{
char ch;
printf("\n Input data\n");
do
{
printf("\n The name of the object: "); scanf("%s",t1.title);
printf("\n Year of study : "); scanf("%f",t1.year);
printf(" Name of the faculty: "); scanf("%s",&t1.faculty);
printf(" Name of the cathedra : "); scanf("%s",&t1.cathedra);
printf(" Number of hours : "); scanf("%d",&t1.kol);
fwrite(&t1,sizeof(t1),1,tf);
printf(" Finish input? y/n ");
ch=getch();}
while (ch != 'y');
fclose(tf);
}
void desh(FILE *tf){
char a[4];
float max=500, s;
int i,b,c,d;
fread(&t1,sizeof(t1),1,tf);
while (!feof(tf)){
if(t1.kol>max){
max=t1.kol;
a=t1.title; // line 89
b=t1.year;
c=t1.faculty; // line 91
d=t1.cathedra; // line 92
s=t1.kol;
}
}
printf("Max number of hours:\n");
printf("\n The name of the object: %s Year of study: %f Name of the faculty: %s Name of the cathedra: %s,Number of hours : %d",a,b,c,d,s);
fclose(tf);
}
解决方案
- 您需要使结构中的字符串足够大以容纳输入。我用30以下。
- 您必须使用
%d
输入和输出年份,而不是%f
,因为它是int
. - 您必须
&
在 in 的整数变量之前使用scanf()
,不应该将其与字符串一起使用。 - 变量
a
、c
和d
需要声明为char
数组,以便您可以将字符串从结构复制到它们。 - 您必须使用
strcpy()
来复制字符串,而不是赋值。 - 不要使用
while (!feof(tf))
循环输入。使用while (fread(&t1,sizeof(t1),1,tf))
.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define STRSIZE 30
struct object{
char title[STRSIZE];
int year,kol;
char faculty[STRSIZE];
char cathedra[STRSIZE];
} t1;
void input(FILE *);
void print(FILE *);
void app(FILE *);
void desh(FILE *);
main(){
char c;
FILE *tf;
while (1){
puts(" 1 Input data");
puts(" 2 Output the number of hours");
puts(" 3 Exit");
c=getch();
switch(c)
{
case '1': input(tf); break;
case '2': desh(tf); break;
case '3': return 0;
default : puts(" Wrong mode");
}
}
}
void input(FILE *tf)
{
char ch;
printf("\n Input data\n");
do
{
printf("\n The name of the object: "); scanf("%s",t1.title);
printf("\n Year of study: "); scanf("%d",&t1.year);
printf("\n Name of the faculty: "); scanf("%s",t1.faculty);
printf("\n Name of the cathedra: "); scanf("%s",t1.cathedra);
printf("\n Number of hours: "); scanf("%d",&t1.kol);
fwrite(&t1,sizeof(t1),1,tf);
printf("\n Finish input? y/n ");
ch=getch();
}
while (ch != 'y');
fclose(tf);
}
void print(FILE *tf){
int i;
i=1;
;
while (fread(&t1,sizeof(t1),1,tf))
{
printf("\n The name of the object: %s, Year of study: %d, Name of the faculty: %s, Name of the cathedra: %s, Number of hours:%d", i,t1.title,t1.year,t1.faculty,t1.cathedra,t1.kol);
fread(&t1,sizeof(t1),1,tf);
i++;
}
}
void app(FILE *tf)
{
char ch;
printf("\n Input data\n");
do
{
printf("\n The name of the object: "); scanf("%s",t1.title);
printf("\n Year of study : "); scanf("%f",t1.year);
printf(" Name of the faculty: "); scanf("%s",&t1.faculty);
printf(" Name of the cathedra : "); scanf("%s",&t1.cathedra);
printf(" Number of hours : "); scanf("%d",&t1.kol);
fwrite(&t1,sizeof(t1),1,tf);
printf(" Finish input? y/n ");
ch=getch();}
while (ch != 'y');
fclose(tf);
}
void desh(FILE *tf){
char a[STRSIZE];
int max=500, s;
char c[STRSIZE];
char d[STRSIZE];
while (fread(&t1,sizeof(t1),1,tf)){
if(t1.kol>max){
max=t1.kol;
strcpy(a, t1.title);
b=t1.year;
strcpy(c, t1.faculty);
strcpy(d, t1.cathedra);
s=t1.kol;
}
}
printf("Max number of hours:\n");
printf("\n The name of the object: %s Year of study: %d Name of the faculty: %s Name of the cathedra: %s,Number of hours : %d",a,b,c,d,s);
fclose(tf);
}
推荐阅读
- c# - CRM 工作流程:如何从子网格中获取所有记录作为实体列表并执行多重检索
- jdbc - 存储过程调用失败并出现“不支持功能”错误
- c# - 点网 2.1
使用变量标记 - visual-c++ - 具有透明但可点击的外边框的子框架
- javascript - Javascript .match() 计算括号数(已解决,谢谢!)
- excel - 如何为 hh:mm:ss:ff 格式设置前导零?
- c - 一个指针可以指向C中的多个指针吗?
- javascript - 在javascript中的函数内声明变量
- r - R中受监督的LDA包的说明
- https - Traefik 不会从 Kubernetes 的入口定义中选择 TLS 证书机密