gpt4 book ai didi

c - 编程错误 - 名称被截断,无法将参数 1 从 'int' 转换为 'char *'

转载 作者:行者123 更新时间:2023-11-30 15:02:52 25 4
gpt4 key购买 nike

您好,我的作业需要一些帮助。我是编程新手。我遇到了该程序的一些错误。

  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() 函数中传递了需要字符数据的整数参数。

我修改了你的代码,看看吧:-

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

关于c - 编程错误 - 名称被截断,无法将参数 1 从 'int' 转换为 'char *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40925200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com