无法弄清楚如何修复编译器错误。 (简单的初学者课程)

时间:2015-03-25 21:17:31

标签: c struct compilation

尝试在Codeblocks中编译时出现以下错误:

main.c|94|error: expected expression before '==' token|
main.c|99|error: expected expression before '==' token|
main.c|100|error: 'title' undeclared (first use in this function)|
main.c|100|note: each undeclared identifier is reported only once for each function it appears in|
main.c|119|error: incompatible types when assigning to type 'char[3]' from type 'char *'|
main.c|133|error: expected expression before '==' token|
main.c|138|error: expected expression before '==' token|
main.c|159|error: incompatible types when assigning to type 'char[3]' from type 'char *'|
main.c|188|error: expected expression before 'int'|
main.c|201|error: expected expression before '==' token|
main.c|206|error: expected expression before '==' token|
||=== Build failed: 10 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

这是我的代码。

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define LENGTH == 30;

typedef struct {
    char div[3];
    short dept, num;
    char credits[3];
    char title[30];
} course;

int main()
{
    printf("Welcome to the Ultimate Course Database App!\n");

    int input;
    char filenameOne[20];
    char filenameTwo[20];

    printf("Please type in the name of the database file:\n");
    scanf("%s", &filenameOne);

    FILE *in_file  = fopen(filenameOne, "r");

    //If the file does not exist
    if (!in_file)
    {
        printf("The file you entered does not exist. A new file with the same name will be created for you.\n");

        //Create the file
        FILE *in_file = fopen(filenameOne, "w");
        course Course;
        for (int counter=0; counter <= 559999; counter++)
        {
            Course.dept == 000;
            Course.num == 000;
            fwrite(&Course, sizeof(course), 1, in_file);
        }
    }

    do
    {
        fseek(in_file, 0, SEEK_SET);

        printf("Please choose from the options below, by typing in the corresponding number.\n");

        printf("1: Create new courses from a plain text file, adding them to the database.\n");
        printf("2: Add a single course to the database.\n");
        printf("3: Display all the details of a particular course.\n");
        printf("4: Display all the actual courses in the database.\n");
        printf("5: Change the name of a course in the database.\n");
        printf("6: Delete a course from the database.\n");
        printf("7: Add a course to a semester course listing.\n");
        printf("8: Remove a course from a semester course listing.\n");
        printf("9: Display a semester course listing, in course number order.\n");
        printf("10: Quit.\n");

        scanf("%d", &input);

        //If Option 1 is selected
        if (input == 1) {
            printf("You selected Option 1: Create new courses from a plain text file, adding them to the database.\n");
            printf("Please type in the name of your text file below.\n");
            scanf("%s", &filenameTwo);
            FILE *in_file_text  = fopen(filenameTwo, "r");
            if (!in_file_text) {
                fprintf(stderr, "File not found.\n");
            }
            while (feof(in_file_text)) {
                //Joanne's code
                int c1, c2;
                char div[3], title[LENGTH+1];
                int dept, num;
                int count = 0;
                int c = ' ';
                fscanf(in_file_text, "%2s.%d.%d %d.%d ", div, &dept, &num, &c1, &c2);
                while (count < LENGTH && (c = fgetc(in_file_text)) != '\n') {
                    title[count++] = (char) c;
                }
                title[count] = '\0';
                while (c != '\n') c = fgetc(in_file_text);

                //error checking
                if (!(dept<701 && dept>001 && num<899 && num>100 && (c1 == 0 || c1 == 1 || c1 == 5) && (c2 == 0 || c2 == 1 || c2 == 5))) {
                    fprintf(stderr, "Invalid input.\n");
                }

                course ToVerify;
                fseek(in_file, ((dept)*700)+(num-100), SEEK_SET);
                fread(&ToVerify, sizeof(course), 1, in_file);

                //check to see if empty course or not
                if (ToVerify.dept == 000 && ToVerify.num == 000) {
                    course ToAdd;
                    ToAdd.dept = dept;
                    ToAdd.num = num;
                    ToAdd.div = div;
                    ToAdd.title = title;
                    fwrite(&ToAdd, sizeof(course), 1, in_file);
                }
            }
            fclose(in_file_text);
        }

        //If option 2 is selected
        if (input == 2) {
            printf("You selected Option 2: Add a single course to the database.\n");
            printf("Please type in the course below.\n");
            //Joanne's code
            int c1, c2;
            char div[3], title[LENGTH+1];
            int dept, num;
            int count = 0;
            int c = ' ';
            fscanf(stdin, "%2s.%d.%d %d.%d ", div, &dept, &num, &c1, &c2);
            while (count < LENGTH && (c = fgetc(stdin)) != '\n') {
                title[count++] = (char) c;
            }
            title[count] = '\0';
            while (c != '\n') c = fgetc(stdin);

            //do the whole thing from 1 all over again
            //error checking
            if (!(dept<701 && dept>001 && num<899 && num>100 && (c1 == 0 || c1 == 1 || c1 == 5) && (c2 == 0 || c2 == 1 || c2 == 5))) {
                fprintf(stderr, "Invalid input.\n");
            }

            course ToVerify;
            fseek(in_file, ((dept)*700)+(num-100), SEEK_SET);
            fread(&ToVerify, sizeof(course), 1, in_file);

            //check to see if empty course or not
            if (ToVerify.dept == 000 && ToVerify.num == 000) {
                course ToAdd;
                ToAdd.dept = dept;
                ToAdd.num = num;
                ToAdd.div = div;
                ToAdd.title = title;
                fwrite(&ToAdd, sizeof(course), 1, in_file);
            }
        }

        //If option 3 is selected
        if (input == 3) {
            printf("You selected Option 3: Display all the details of a particular course.\n");
            printf("Please type in the department and course number below.\n");
            int dept, num;
            fscanf(stdin, "%d.%d", &dept, &num);

            // check for input error
            if (!(dept<701 && dept>001 && num<899 && num>100)) {
                fprintf(stderr, "Invalid input.\n");
            }
            course ToDisplay;
            fseek(in_file, ((dept)*700)+(num-100), SEEK_SET);
            fread(&ToDisplay, sizeof(course), 1, in_file);
            printf("%2s.%d.%d %d.%d ", ToDisplay.div, ToDisplay.dept, ToDisplay.num, ToDisplay.credits, ToDisplay.title);
        }

        //If Option 4 is selected
        if (input == 4) {
            printf("You selected Option 4: Display all courses.\n");
            while (int counter = 0, counter < 560000, counter++) {
                course ToDisplay;
                fread(&ToDisplay, sizeof(course), 1, in_file);
                if (ToDisplay.dept != 000 && ToDisplay.num != 000) {
                    printf("%2s.%d.%d %d.%d ", ToDisplay.div, ToDisplay.dept, ToDisplay.num, ToDisplay.credits, ToDisplay.title);
                }
            }
        }

        //If option 5 is selected
        if (input == 5) {
            printf("You selected Option 5: Change the name of a course.\n");
            printf("Please type in the department and course number below, with the new name\n");
            char title[LENGTH+1];
            int dept, num;
            int count = 0;
            int c = ' ';
            fscanf(stdin, "%2s.%d.%d", div, &dept, &num);
            while (count < LENGTH && (c = fgetc(fp)) != '\n') {
                title[count++] = (char) c;
            }
            title[count] = '\0';
            while (c != '\n') c = fgetc(stdin);

            //check for input error DO
            if (!(dept<701 && dept>001 && num<899 && num>100)) {
                fprintf(stderr, "Invalid input.\n");
            }

            course ToChange;
            fseek(in_file, ((dept)*700)+(num-100), SEEK_SET);
            fread(&ToChange, sizeof(course), 1, in_file);
            if (ToChange.dept != 000 && ToChange.num != 000) {
                ToChange.title = title;
                fwrite(&ToChange, sizeof(course), 1, in_file);
            }
            if (ToChange.dept == 000 && ToChange.num == 000) {
                fprintf(stderr, "Record id empty!");
            }
        }

        //If Option 6 is selected
        if (input == 6) {
            printf("You selected Option 6: Delete a course.\n");
            printf("Please type in the department and course number below.\n");
            int dept, num;
            fscanf(stdin, "%d.%d", &dept, &num);

            //check for input error DO
            if (!(dept<701 && dept>001 && num<899 && num>100)) {
                fprintf(stderr, "Invalid input.\n");
            }

            course ToDelete;
            fseek(in_file, ((dept)*700)+(num-100), SEEK_SET);
            fread(&ToDelete, sizeof(course), 1, in_file);
            if (ToDelete.dept != 000 && ToDelete.num != 000) {
                ToDelete.dept == 000;
                ToDelete.num == 000;
                ToDelete.div == "";
                ToDelete.credits == "";
                ToDelete.title == "";
                fwrite(&ToDelete, sizeof(course), 1, in_file);
            }
            if (ToDelete.dept == 000 && ToDelete.num == 000) {
                fprintf(stderr, "Record id empty!\n");
            }
        }
    }
    while (input < 10);
    fclose(in_file);
}

1 个答案:

答案 0 :(得分:1)

define语法不正确,正确的语法是:

#define LENGTH 30

这里有=和==不匹配:

Course.dept = 000;
Course.num = 000;

您使用的是C99吗?除非是这种情况,否则for循环中的声明是错误的:

for (int counter=0; counter <= 559999; counter++)

不要使用= here:

指定静态字符[X]
ToAdd.div = div;
ToAdd.title = title;
必须使用

strcpy或strncpy或直接将char写入struct字段。

for循环的循环instrad格式错误(C99语法):

while (int counter = 0, counter < 560000, counter++) {

fp变量在这里未知:

while (count < LENGTH && (c = fgetc(fp)) != '\n') {

尝试构建以逐步编写代码并编译它以尽快检测每个错误。一旦解决了#define,每个错误都会显示其行,您只需构建,修复第一个错误,构建,修复第二个错误等等:

In function ‘main’:
101:31: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
102:33: error: incompatible types when assigning to type ‘char[30]’ from type ‘char *’
141:27: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
142:29: error: incompatible types when assigning to type ‘char[30]’ from type ‘char *’
167:20: error: expected expression before ‘int’
185:49: error: ‘fp’ undeclared (first use in this function)
185:49: note: each undeclared identifier is reported only once for each function it appears in
200:32: error: incompatible types when assigning to type ‘char[30]’ from type ‘char *’

之后你必须测试你的程序。