2016-12-02 8 views
0

こんにちは。私はプログラミングの初心者です。私はプログラムのいくつかのエラーにぶつかった。プログラミングエラー - 名前が切り捨てられ、パラメータ1を 'int'から 'char *'に変換できません

  1. 名はエラーにヒットが '*文字' から 'int型からパラメータ1を変換することはできません番号
  2. に切り捨てられ

 

#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; 
} 
+0

あなたが正確なエラーメッセージを切り取って貼り付けることができますしてください。最初の点は意味をなさないし、行番号がなければ、2番目の問題がどこに発生するのかわからない。 –

+1

これは、MCVE([MCVE])になるにはかなりのコードです。しかし、おそらくあなたが経験を持っていないので、それを減らすことはむしろ難しいでしょう。コードの部分を個別にテストする方法について考えてみましょう。 –

+1

コンパイラのエラー[そのまま](https://en.wikipedia.org/wiki/Verbatim)を質問にコピーして、エラーの性質をより明確にすることをお勧めします。つまり、通常は自分でコンパイルする必要はありません。 –

答えて

1

あなたのコードは、3つの問題を持っています: -

  1. 楽しいction "writeFile()"あなたが不明な型構造体名を渡しました。あなたはStudentの前にstructを書くのをスキップしました。
  2. inValid1()およびInvalid2()関数の定義が見つかりませんでした。
  3. charデータが必要なinValid2()関数で整数引数を渡しました。私はあなたのコードを変更した

ただ見ている: -

#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; 
} 
関連する問題