簡體   English   中英

編程錯誤-名稱被截斷並且無法將參數1從'int'轉換為'char *'

[英]Programming error - name truncated and cannot convert parameter 1 from 'int' to 'char *'

嗨,我需要一些幫助來完成我的任務。 我是編程的新手。 我在程序中遇到了一些錯誤。

  1. 名稱被截斷為數字
  2. 命中錯誤無法將參數1從'int'轉換為'char *'

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //for strtok
#include <ctype.h>

//function prototypes
struct Student* readFile(int*, struct Student*);
char *trimwhitespace(char *str);
void listRecords(struct Student*, int);
void writeFile(struct Student*, int);
int isValid1(char *);
int isValidatt(char *);
int isValidatt2(char [10][10]);
int isValid3(char *);

struct Student {
    char SID [10];
    char name[30];
    char code [20];
    char attendance [10][10] ;
    int test1;
    int test2;
    int tutorial;
    int exam;
};




int main() {
    int numStudents = 0;
    int choice;
    struct Student *students; //array of students

    students = (struct Student *) malloc(sizeof(struct Student));
    //create one empty slot 

    do {
        printf("[1] Read from file \n");
        printf("[2] List records \n");
        printf("[3] Write to file \n");
        printf("[4] Exit \n");
        printf("Choice: ");
        scanf("%i", &choice);

        switch(choice) {
        case 1:
            printf("Reading from file .. \n");
            students = readFile(&numStudents, students);
            printf("Read from file %i customers\n", numStudents);
            break;
        case 2:
            printf("Listing records .. \n");
            listRecords(students, numStudents);
            break;
        case 3:
            printf("Writing to file .. \n");
            writeFile(students, numStudents);
            break;
        case 4:
            printf("Bye .. \n");
            break;
        }

    } while(choice != 4);


    return 0;
}


struct Student* readFile(int *numStudents, struct Student *students) {
    FILE *inFile; //file handle
    char filename[50];
    char line[255]; //for each line
    int i = 0, j;
    char *token;

    printf("Enter filename: "); //prompt user for filename
    scanf("%s", filename);

    //check that file can be found and opened
    if ((inFile = fopen(filename, "r")) == NULL) { 
        printf("Open failed : %s\n", filename) ;    
        exit(1) ;
    }

    //create one empty slot
    students = (struct Student *) malloc(sizeof(struct Student));

    //reading file, line by line
    while (fgets(line,255, inFile)!=NULL) {

        token = strtok(line, ","); //comma is the delimiter/separator
        strcpy(students[i].SID, trimwhitespace(token));
        printf("%s\n", students[i].SID);

        token = strtok(NULL, ","); //get next token -> name
        strcpy(students[i].name, trimwhitespace(token));
        printf("%s\n", students[i].name);

        token = strtok(NULL, ","); //get next token -> class code
        strcpy(students[i].code, trimwhitespace(token));
        printf("%s\n", students[i].code);

        for(j = 0; j < 10; j++) {
            token = strtok(NULL, ","); //get next token -> attendance
            strcpy(students[i].attendance[j], trimwhitespace(token));
            printf("%s\n", students[i].attendance[j]);
        }

        token = strtok(NULL, ","); //get next token -> test 1
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].test1 = atoi(trimwhitespace(token));
        printf("%d\n", students[i].test1);

        token = strtok(NULL, ","); //get next token -> test 1
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].test2 = atoi(trimwhitespace(token));
        printf("%d\n", students[i].test2);

        token = strtok(NULL, ","); //get next token -> tutorial
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].tutorial = atoi(trimwhitespace(token));
        printf("%d\n", students[i].tutorial);

        token = strtok(NULL, ","); //get next token -> exam
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].exam = atoi(trimwhitespace(token));
        printf("%d\n", students[i].exam);

        i++;

        //resize it to add in one extra slot
        students = (struct Student *) realloc(students, (i+1)*sizeof(struct Student));
    }

    fclose(inFile); //close the file
    *numStudents = i;

    return students;
}


void listRecords(struct Student *students, int n) {
    int i;

    if(n == 0) {
        printf("No Student in list .. \n");
    } else {

        for(i = 0; i < n; i++) {

            printf("[Student #%d]\n", i+1);
            printf("%s %s %s %s %s %s %s %s %s %s %s %s %d %d %d %d \n", 
                students[i].SID, students[i].name, students[i].code, students[i].attendance  [0], students[i].attendance  [1], students[i].attendance  [2], students[i].attendance  [3], students[i].attendance  [4], 
                students[i].attendance  [5], students[i].attendance  [6], students[i].attendance  [7], students[i].attendance  [8], students[i].test1, students[i].test2, students[i].tutorial, students[i].exam, "\n");
        }
    }
}

void writeFile(struct Student *students, int n) {
    FILE *inFile1;
    FILE *inFile2;
    char filename1[30] = "EN0273-Errors(LeeAiWen).txt";
    char filename2[30] = "EN0273-Valid(LeeAiWen).csv";
    int i, valid;
    Student s;

    if ((inFile1 = fopen(filename1, "w")) == NULL)
    {
        printf("Open failed : %s\n", filename1) ;
        exit(1) ;
    }

    if ((inFile2 = fopen(filename2, "w")) == NULL)
    {
        printf("Open failed : %s\n", filename1) ;
        exit(1) ;
    }

    valid = 1;
    for(i = 0; i < n; i++) {
        s = students[i];


        if(isValid1(s.SID) == 0) {
            fprintf(inFile1, "%s %s %s \n", s.SID, s.name, s.code);
        }else {fprintf(inFile2, "%s %s %s \n", s.SID, s.name, s.code);}

        if(isValidatt2(s.attendance) == 0) {
            fprintf(inFile1, "%s %s %s \n", s.SID, s.name, s.code);
        }else {fprintf(inFile2, "%s %s %s \n", s.SID, s.name, s.code);}

        if(isValid3(s.test2) == 0) {
            fprintf(inFile1, "%s %s %s \n", s.SID, s.name, s.code);
        }else {fprintf(inFile2, "%s %s %s \n", s.SID, s.name, s.code);}

/*
        if(valid == 0) {
            fprintf(inFile1, "%s %s %s \n", s.SID, s.name, s.code);
        } else {
            fprintf(inFile2, "%s %s %s \n", s.SID, s.name, s.code);
        }           
    */
    }

    fclose(inFile1);
    fclose(inFile2);

}
int isValid3( char *id) { //returns 1 if valid, otherwise 0
    int i;


    if(atoi (id) > 60) { return 0; } // used 'atoi' to convert 'id' to int to compare with '60' which is an int value, marks should not be more than 60 max

    for(i = 0; i < 2; i++) {    // only 2 digits are allowed and no characters
        if(isdigit(id[i]) == 0) { return 0; }
    }

    return 1;
}



char *trimwhitespace(char *str)
{
    char *end;

    // Trim leading space
    while(isspace((unsigned char)*str)) str++;

    if(*str == 0)  // All spaces?
        return str;

    // Trim trailing space
    end = str + strlen(str) - 1;
    while(end > str && isspace((unsigned char)*end)) end--;

    // Write new null terminator
    *(end+1) = 0;

    return str;
}

您的代碼有3個問題:-

  1. 在函數“ writeFile()”中,您傳遞了未知的類型結構名稱。 您跳過了在Student之前編寫struct的過程;
  2. 找不到inValid1()和Invalid2()函數的定義。
  3. 您在inValid2()函數中傳遞了整數參數,該參數需要char數據。

我修改了您的代碼,只是看一下:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //for strtok
#include <ctype.h>

//function prototypes
struct Student* readFile(int*, struct Student*);
char *trimwhitespace(char *str);
void listRecords(struct Student*, int);
void writeFile(struct Student*, int);
int isValid1(char *);
int isValidatt(char *);
int isValidatt2(char [10][10]);
int isValid3(int *);

struct Student {
    char SID [10];
    char name[30];
    char code [20];
    char attendance [10][10] ;
    int test1;
    int test2;
    int tutorial;
    int exam;
};




int main() {
    int numStudents = 0;
    int choice;
    struct Student *students; //array of students

    students = (struct Student *) malloc(sizeof(struct Student));
    //create one empty slot 

    do {
        printf("[1] Read from file \n");
        printf("[2] List records \n");
        printf("[3] Write to file \n");
        printf("[4] Exit \n");
        printf("Choice: ");
        scanf("%i", &choice);

        switch(choice) {
        case 1:
            printf("Reading from file .. \n");
            students = readFile(&numStudents, students);
            printf("Read from file %i customers\n", numStudents);
            break;
        case 2:
            printf("Listing records .. \n");
            listRecords(students, numStudents);
            break;
        case 3:
            printf("Writing to file .. \n");
            writeFile(students, numStudents);
            break;
        case 4:
            printf("Bye .. \n");
            break;
        }

    } while(choice != 4);
    return 0;
}


struct Student* readFile(int *numStudents, struct Student *students) {
    FILE *inFile; //file handle
    char filename[50];
    char line[255]; //for each line
    int i = 0, j;
    char *token;

    printf("Enter filename: "); //prompt user for filename
    scanf("%s", filename);

    //check that file can be found and opened
    if ((inFile = fopen(filename, "r")) == NULL) {
        printf("Open failed : %s\n", filename) ;
        exit(1) ;
    }

    //create one empty slot
    students = (struct Student *) malloc(sizeof(struct Student));

    //reading file, line by line
    while (fgets(line,255, inFile)!=NULL) {

        token = strtok(line, ","); //comma is the delimiter/separator
        strcpy(students[i].SID, trimwhitespace(token));
        printf("%s\n", students[i].SID);

        token = strtok(NULL, ","); //get next token -> name
        strcpy(students[i].name, trimwhitespace(token));
        printf("%s\n", students[i].name);

        token = strtok(NULL, ","); //get next token -> class code
        strcpy(students[i].code, trimwhitespace(token));
        printf("%s\n", students[i].code);

        for(j = 0; j < 10; j++) {
            token = strtok(NULL, ","); //get next token -> attendance
            strcpy(students[i].attendance[j], trimwhitespace(token));
            printf("%s\n", students[i].attendance[j]);
        }

        token = strtok(NULL, ","); //get next token -> test 1
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].test1 = atoi(trimwhitespace(token));
        printf("%d\n", students[i].test1);

        token = strtok(NULL, ","); //get next token -> test 1
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].test2 = atoi(trimwhitespace(token));
        printf("%d\n", students[i].test2);

        token = strtok(NULL, ","); //get next token -> tutorial
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].tutorial = atoi(trimwhitespace(token));
        printf("%d\n", students[i].tutorial);

        token = strtok(NULL, ","); //get next token -> exam
        strcpy(students[i].name, trimwhitespace(token));
        //students[i].exam = atoi(trimwhitespace(token));
        printf("%d\n", students[i].exam);

        i++;
        //resize it to add in one extra slot
        students = (struct Student *) realloc(students, (i+1)*sizeof(struct Student));
    }

    fclose(inFile); //close the file
    *numStudents = i;

    return students;
}


void listRecords(struct Student *students, int n) {
    int i;

    if(n == 0) {
        printf("No Student in list .. \n");
    } else {

        for(i = 0; i < n; i++) {

            printf("[Student #%d]\n", i+1);
            printf("%s %s %s %s %s %s %s %s %s %s %s %s %d %d %d %d \n",
                students[i].SID, students[i].name, students[i].code, students[i].attendance[0], students[i].attendance[1], students[i].attendance[2], students[i].attendance[3], students[i].attendance[4],
                students[i].attendance[5], students[i].attendance[6], students[i].attendance[7], students[i].attendance[8], students[i].test1, students[i].test2, students[i].tutorial, students[i].exam, "\n");
        }
    }
}

void writeFile(struct Student *students, int n) {
    FILE *inFile1;
    FILE *inFile2;
    char filename1[30] = "EN0273-Errors(LeeAiWen).txt";
    char filename2[30] = "EN0273-Valid(LeeAiWen).csv";
    int i, valid;
    struct Student s;

    if ((inFile1 = fopen(filename1, "w")) == NULL)
    {
        printf("Open failed : %s\n", filename1) ;
        exit(1) ;
    }

    if ((inFile2 = fopen(filename2, "w")) == NULL)
    {
        printf("Open failed : %s\n", filename1) ;
        exit(1) ;
    }

    valid = 1;
    for(i = 0; i < n; i++) {
        s = students[i];


  /*      if(isValid1(s.SID) == 0) {
            fprintf(inFile1, "%s %s %s \n", s.SID, s.name, s.code);
        }else {fprintf(inFile2, "%s %s %s \n", s.SID, s.name, s.code);}

        if(isValidatt2(s.attendance) == 0) {
            fprintf(inFile1, "%s %s %s \n", s.SID, s.name, s.code);
        }else {fprintf(inFile2, "%s %s %s \n", s.SID, s.name, s.code);}
*/
        if(isValid3(&s.test2) == 0) {
            fprintf(inFile1, "%s %s %s \n", s.SID, s.name, s.code);
        }else {fprintf(inFile2, "%s %s %s \n", s.SID, s.name, s.code);}

/*
    }

    fclose(inFile1);
    fclose(inFile2);

}
int isValid3(int *id) { //returns 1 if valid, otherwise 0
    int i;


    if(*id > 60) { return 0; } // used 'atoi' to convert 'id' to int to compare with '60' which is an int value, marks should not be more than 60 max

    for(i = 0; i < 2; i++) {    // only 2 digits are allowed and no characters
        if(id[i] == 0) { return 0; }
    }

    return 1;
}



char *trimwhitespace(char *str)
{
    char *end;

    // Trim leading space
    while(isspace((unsigned char)*str)) str++;

    if(*str == 0)  // All spaces?
        return str;

    // Trim trailing space
    end = str + strlen(str) - 1;
    while(end > str && isspace((unsigned char)*end)) end--;

    // Write new null terminator
    *(end+1) = 0;

    return str;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM