首页 > 解决方案 > 结构[对象] 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);
}

标签: c++

解决方案


  1. 您需要使结构中的字符串足够大以容纳输入。我用30以下。
  2. 您必须使用%d输入和输出年份,而不是%f,因为它是int.
  3. 您必须&在 in 的整数变量之前使用scanf(),不应该将其与字符串一起使用。
  4. 变量acd需要声明为char数组,以便您可以将字符串从结构复制到它们。
  5. 您必须使用strcpy()来复制字符串,而不是赋值。
  6. 不要使用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);
}

推荐阅读