本文介绍了怎么了?我不能添加......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Ok这是一个问题,我有一个虚构的数据库程序,我需要 代码,添加一个我有函数inser_patient的病人。但是当我想要输入详细信息时,我不会按照我想要的方式工作。 代码: #include< stdio.h> #include< stdlib.h> #include< time.h> #include< string.h> #define MAXPATIENTS 20 struct details { int id; char forename [20]; char initial; char surname [20]; int day_of_entry; int max_wait; }; struct details患者[MAXPATIENTS]; int npatients = 0; int insert_patient(int index,struct details newpatient); int main(void){ int选择; int id; int loop = 1; int n = 0; int error = 0; int limit; int Qnum; FILE * fptr; //将文件指针声明为fptr fptr = fopen(" queue.dat"," r"); //打开queue.dat到fptr //读取 while(fscanf(fptr,"%d%s%c%s%d%d [^ \\ \\ n \\ n \\ n \\ n \\ n \\ n \\ n&n;& \\ n \\ n患者[n] .id, 患者[n] .forename,& patient [n] .initial,patient [n]。姓氏, & patient [n] .day_of_entry,& patient [n] .max_wait)!= EOF){ n ++; } fclose(fptr); //循环继续程序 while(loop!= 0){ //保持程序运行 //显示菜单 printf(" \ n\ n"); printf("%) d \ n",day_now()); printf(" _________________________________________ \ n"); printf(" | ------- ---- NHS队列控制----------- | \ n"); printf(" |创建新患者\t-按1 \ t | \ n"); printf(" |删除患者\t-按2 \t | \ n"); printf(" |找到病人\ t \ t-按3 \ t | \ n); printf(" | List Queue \t\t- Press 4 \t | \ n"); printf(" |治疗下一位患者\t-按5 \ t | \ n"); printf(" | Quit \t\t\t-按6 \t | \ n"); printf(" | _______________________________________ | \ n"); printf(" Choice:"); scanf("%d",& Choice) ); printf(" \ n"); //做出选择,执行功能 开关(选择){ //新病人 案例1: limit = n; if(limit< 21){ list_queue(); printf(" \ n请输入以下详细信息\ n"); printf("什么队列号你想把这个病人 给?\ n"); scanf("%d"& Qnum); printf(患者ID:); scanf("%d",& patient [20] .id); printf (" Forename:"); scanf("%s",patient [20] .forename); printf(" Middle Initial:" ); scanf("%c",& patient [20] .initial); printf(" Surname:"); scanf(" %s",patient [20]。姓氏); printf(最长等待时间:); scanf("%d", & patient [20] .max_wait); 患者[20] .day_of_entry = day_now(); error = insert_patient(Qnum,patient [20]); if(error == -1){ printf(错误 - 患者存在!\ n); } else { printf(" \ nPatient Inserted!\ n"); } } else { printf(" Array is Full!\ n"); } break; } 返回0; } //这是添加功能 int insert_patient(int index,struct details newpatient){ int i = 0; int n = 0; int y = find_patient_id(index); int x; int error; FILE * FPTR; //将文件指针声明为fptr fptr = fopen(" queue.dat"," r"); while(fscanf(fptr,") ;%d%s%c%s%d%d [^ \ n]",& patient [n] .id, patient [n] .forename,& patient [ n] .initial,patient [n]。姓, & patient [n] .day_of_entry,& patient [n] .max_wait)!= EOF){ n ++; } fclose(fptr); if(y == -1){ fptr = fopen(" queue.dat"," w"); for(i = 0; i< = index-2; i ++){ fprintf(fptr,"%d",patient [i] .id); fprintf(fptr,"%s",patient [i] .forename); fprintf(fptr,"%c",patient [i] .initial); fprintf(fptr,"%s",patient [i]。姓氏)​​; fprintf(fptr,"%d",patient [i] .day_of_entry); fprintf(fptr,"%d \ n" ,耐心[i] .max_wait); } fprintf(fptr,"%d",patient [20] .id); fprintf(fptr,"%s",patient [20] .forename); fprintf(fptr,"%c",patient [20] .initial); fprintf(fptr,"%s",patient [20] .surname); fprintf(fptr,"%d",patient [20] .day_of_entry); fprintf(fptr,"%d \ n",patient [20] .max_wait); for(x = index; x< = n + 1; x ++){ fprintf(fptr,"%d",patient [x] .id); fprintf(fptr,"%s ",patient [x] .forename); fprintf(fptr,"%c",patient [x] .initial); fprintf(fptr, %s,患者[x]。姓氏); fprintf(fptr,%d,患者[x] .day_of_entry); fprintf(fptr,"%d \ n",patient [x] .max_wait); } error = 0; } else if(y!= 0){ error = -1; } fclose( fptr); 返回错误; } 解决方案 我们应该猜猜你在说什么吗?你想要什么?b $ b想要什么?究竟发生了什么? 你应该验证fopen是否成功。 这是错误的测试。 fscanf仅在没有数据转换时才返回EOF。只有当返回值为6时才应该继续。 你永远不会检查你的 数组中元素数量是否溢出。 你永远不会使用限制,所以你为什么要关心它的价值是什么。 此功能的范围没有原型。 在缓冲系统上,调用不以''\ n'结尾的printf可能不会出现在系统等待输入之前。如果您希望输入 与提示符出现在同一行,您应该添加 fflush(stdout); 以确保缓冲区被刷新到流中。 每次使用患者[20]都会调用未定义的行为。有效的 下标是0到19(MAXPATIENTS-1)。 这将导致以后对scanf的任何调用出现问题。要输入初始的,你必须按两个键,一个用于写字母, 一个用于输入。这将导致输入 流中的两个字符,一个用于字母,另一个用于输入'\\ n'。 %c只会消耗这封信。消费这封信。 ''\ n''将保留在流中并提前终止 下一个scanf。 虽然传递结构是完全合法的,但通常的建议是,如果结构为 大一点琐碎。 此函数的范围没有原型。你也忘了 提供这个函数的定义。 你真的认为这个评论包含任何有用的信息吗? 您已经将记录读回到患者身上。为什么你再次阅读它们?$ b $ class =post_quotes> > n ++; } fclose(fptr); if(y == -1){ fptr = fopen(" queue.dat"," w(); for(i = 0; i< = index-2; i ++){ fprintf(fptr,"%d",patient [i] .id); fprintf(fptr,"%s",patient [i] .forename); fprintf(fptr,"%c",patient [i] .initial); 你写的记录的字段没有插入字符 。你对fscanf的调用如何知道 forename结束和初始开始的时间? 同义词和两个整数字段之间或day_of_entry 和max_wait之间的数字。 患者[20]仍然不存在。 为什么n + 1?这可以确保您处理无关的数据。幸运的是 (或un-),数据被初始化,因为患者处于文件范围。 你的意思是n-1?这是有道理的,但常见的习语是 i< n,不是我< = n-1。 删除del电子邮件 我们应该猜猜你在说什么吗?你想要什么?b $ b想要什么?究竟发生了什么? 你应该验证fopen是否成功。 这是错误的测试。 fscanf仅在没有数据转换时才返回EOF。只有当返回值为6时才应该继续。 你永远不会检查你的 数组中元素数量是否溢出。 你永远不会使用限制,所以你为什么要关心它的价值是什么。 这个函数的范围没有原型。 在缓冲系统上,调用不以''\ n'结尾的printf可能不会出现在系统等待输入之前。如果您希望输入 与提示符出现在同一行,您应该添加 fflush(stdout); 以确保缓冲区被刷新到流中。 每次使用患者[20]都会调用未定义的行为。有效的 下标为0到19(MAXPATIENTS-1)。 这将导致以后对scanf的任何调用出现问题。要输入初始的,你必须按两个键,一个用于写字母, 一个用于输入。这将导致输入 流中的两个字符,一个用于字母,另一个用于输入'\\ n'。 %c只会消耗这封信。消费这封信。 ''\ n''将保留在流中并提前终止 下一个scanf。 虽然传递结构是完全合法的,但通常的建议是,如果结构为,则传递一个指向结构的指针br /> 大一点琐碎。 此功能的范围没有原型。你也忘了 提供这个函数的定义。 你真的认为这个评论包含任何有用的信息吗? 您已经将记录读回到患者身上。你为什么再次阅读它们?$ block $ class =post_quotes> n ++; } FCLOSE(FPTR); 你写的记录的字段没有插入字符 。你对fscanf的调用如何知道 forename结束和初始开始? 同名和两个整数字段之间或day_of_entry 和max_wait之间同上。 患者[20]仍然不存在。 为什么n + 1?这可以确保您处理无关的数据。幸运的是 (或un-),数据被初始化,因为患者处于文件范围。 你的意思是n-1?这是有道理的,但常见的习语是 i< n,不是我< = n-1。 删除电子邮件的del 当我尝试在患者中输入 max_wait时,如何避免程序跳过[ 20] .max_wait 谢谢 Chris 我们应该猜猜你在说什么吗?你想要什么?b $ b想要什么?究竟发生了什么? You should verify that fopen succeeded. This is the wrong test. fscanf returns EOF only if no data was converted. You should be proceeding only if the return value is 6. You never check for n overflowing the number of elements in your array. You never use limit so why do you care what it’’s value is. There is no prototype in scope for this function. On a buffered system, calls to printf that don’’t end with a ’’\n’’ may not appear before the system waits for input. If you want the input to appear on the same line as the prompt, you should add fflush(stdout); to insure the buffer is flushed to the stream. Every use of patient[20] invokes undefined behavior. The valid subscripts are 0 through 19 (MAXPATIENTS-1). This will cause problems with any following calls to scanf. To enter the initial, you will have to press two keys, one for the letter and one for ENTER. This will result in two characters in the input stream, one for the letter and a ’’\n’’ for the ENTER. The %c will only consume the letter. The ’’\n’’ will stay in the stream and terminate the next scanf prematurely. While it is perfectly legal to pass a struct, the common recommendation is to pass a pointer to the struct if the structure is larger that trivial. There is no prototype in scope for this function. You also forgot to provide the definition of this function. Do you really think this comment contains any useful information? You already read the records into patient back in main. Why are you reading them again? You write the fields of the record with no intervening characters between them. How is your call to fscanf supposed to know where forename ends and initial begins? Ditto between surname and both integer fields or between day_of_entry and max_wait. patient[20] still does not exist. Why n+1? This insures you will process extraneous data. Fortunately (or un-), the data is initialized since patient is at file scope. Did you mean n-1? That would make sense but the common idiom is i < n, not i <= n-1. Remove del for email So how do I avoid the program from skipping when I try to type in the max_wait, in patient[20].max_wait Thanks Chris Ok Here is a problem, I got a imaginary database program that I needto code, to add a patient I have function inser_patient. but when Itry to input the details it doesn''t quite work the way I wanted it to.Code:#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>#define MAXPATIENTS 20struct details {int id;char forename[20];char initial;char surname[20];int day_of_entry;int max_wait;};struct details patient[MAXPATIENTS];int npatients = 0;int insert_patient(int index, struct details newpatient);int main (void) {int Choice;int id;int loop = 1;int n=0;int error=0;int limit;int Qnum;FILE * fptr;//Declares file pointer as "fptr"fptr = fopen("queue.dat", "r");//Opens queue.dat to fptr//Readingwhile (fscanf(fptr,"%d %s %c %s %d %d[^\n]", &patient[n].id,patient[n].forename, &patient[n].initial, patient[n].surname,&patient[n].day_of_entry, &patient[n].max_wait) != EOF){n++;}fclose(fptr);//Loop that continues the programwhile(loop != 0){//To keep program running//Displaying Menuprintf("\n\n");printf("%d \n",day_now());printf("_________________________________________\ n");printf("|-----------NHS Queue Control-----------|\n");printf("| Create New Patient \t- Press 1\t|\n");printf("| Delete Patient \t- Press 2\t|\n");printf("| Find Patient \t\t- Press 3\t|\n");printf("| List Queue \t\t- Press 4\t|\n");printf("| Treate Next Patient \t- Press 5\t| \n");printf("| Quit \t\t\t- Press 6\t|\n");printf("|_______________________________________|\ n");printf("Choice: ");scanf("%d",&Choice);printf("\n");//Choice made, carrying out functionswitch (Choice){//New Patientcase 1:limit = n;if(limit < 21){list_queue();printf("\nPlease enter the following details\n");printf("What Queue Number would you like to place this patientto?\n");scanf("%d", &Qnum);printf("Patient ID: ");scanf("%d", &patient[20].id);printf("Forename: ");scanf("%s", patient[20].forename);printf("Middle Initial: ");scanf("%c ", &patient[20].initial);printf("Surname: ");scanf("%s ", patient[20].surname);printf("Maximum Waiting Time: ");scanf("%d ", &patient[20].max_wait);patient[20].day_of_entry = day_now();error = insert_patient(Qnum, patient[20]);if(error == -1){printf("Error - Patient exists!\n");}else {printf("\nPatient Inserted!\n");}}else {printf("Array is Full!\n");}break;}return 0;}//This is the add functionint insert_patient(int index, struct details newpatient) {int i = 0;int n = 0;int y = find_patient_id(index) ;int x;int error;FILE * fptr;//Declares file pointer as "fptr"fptr = fopen("queue.dat", "r");while (fscanf(fptr,"%d %s %c %s %d %d[^\n]", &patient[n].id,patient[n].forename, &patient[n].initial, patient[n].surname,&patient[n].day_of_entry, &patient[n].max_wait) != EOF){n++;}fclose(fptr);if(y == -1){fptr = fopen("queue.dat", "w");for(i=0;i<=index-2;i++){fprintf(fptr,"%d ",patient[i].id);fprintf(fptr,"%s ",patient[i].forename);fprintf(fptr,"%c ",patient[i].initial);fprintf(fptr,"%s ",patient[i].surname);fprintf(fptr,"%d ",patient[i].day_of_entry);fprintf(fptr,"%d \n",patient[i].max_wait);}fprintf(fptr,"%d ",patient[20].id);fprintf(fptr,"%s ",patient[20].forename);fprintf(fptr,"%c ",patient[20].initial);fprintf(fptr,"%s ",patient[20].surname);fprintf(fptr,"%d ",patient[20].day_of_entry);fprintf(fptr,"%d \n",patient[20].max_wait);for(x=index;x<=n+1;x++){fprintf(fptr,"%d ",patient[x].id);fprintf(fptr,"%s ",patient[x].forename);fprintf(fptr,"%c ",patient[x].initial);fprintf(fptr,"%s ",patient[x].surname);fprintf(fptr,"%d ",patient[x].day_of_entry);fprintf(fptr,"%d \n",patient[x].max_wait);}error = 0;}else if (y != 0){error = -1;}fclose(fptr);return error;} 解决方案Are we supposed to guess what you are talking about? What did youwant? What actually happened?You should verify that fopen succeeded.This is the wrong test. fscanf returns EOF only if no data wasconverted. You should be proceeding only if the return value is 6.You never check for n overflowing the number of elements in yourarray.You never use limit so why do you care what it''s value is.There is no prototype in scope for this function.On a buffered system, calls to printf that don''t end with a ''\n'' maynot appear before the system waits for input. If you want the inputto appear on the same line as the prompt, you should addfflush(stdout);to insure the buffer is flushed to the stream.Every use of patient[20] invokes undefined behavior. The validsubscripts are 0 through 19 (MAXPATIENTS-1).This will cause problems with any following calls to scanf. To enterthe initial, you will have to press two keys, one for the letter andone for ENTER. This will result in two characters in the inputstream, one for the letter and a ''\n'' for the ENTER. The %c will onlyconsume the letter. The ''\n'' will stay in the stream and terminatethe next scanf prematurely.While it is perfectly legal to pass a struct, the commonrecommendation is to pass a pointer to the struct if the structure islarger that trivial.There is no prototype in scope for this function. You also forgot toprovide the definition of this function.Do you really think this comment contains any useful information?You already read the records into patient back in main. Why are youreading them again?You write the fields of the record with no intervening charactersbetween them. How is your call to fscanf supposed to know whereforename ends and initial begins?Ditto between surname and both integer fields or between day_of_entryand max_wait.patient[20] still does not exist.Why n+1? This insures you will process extraneous data. Fortunately(or un-), the data is initialized since patient is at file scope.Did you mean n-1? That would make sense but the common idiom isi < n, not i <= n-1.Remove del for emailAre we supposed to guess what you are talking about? What did youwant? What actually happened?You should verify that fopen succeeded.This is the wrong test. fscanf returns EOF only if no data wasconverted. You should be proceeding only if the return value is 6.You never check for n overflowing the number of elements in yourarray.You never use limit so why do you care what it''s value is.There is no prototype in scope for this function.On a buffered system, calls to printf that don''t end with a ''\n'' maynot appear before the system waits for input. If you want the inputto appear on the same line as the prompt, you should add fflush(stdout);to insure the buffer is flushed to the stream.Every use of patient[20] invokes undefined behavior. The validsubscripts are 0 through 19 (MAXPATIENTS-1).This will cause problems with any following calls to scanf. To enterthe initial, you will have to press two keys, one for the letter andone for ENTER. This will result in two characters in the inputstream, one for the letter and a ''\n'' for the ENTER. The %c will onlyconsume the letter. The ''\n'' will stay in the stream and terminatethe next scanf prematurely.While it is perfectly legal to pass a struct, the commonrecommendation is to pass a pointer to the struct if the structure islarger that trivial.There is no prototype in scope for this function. You also forgot toprovide the definition of this function.Do you really think this comment contains any useful information?You already read the records into patient back in main. Why are youreading them again?You write the fields of the record with no intervening charactersbetween them. How is your call to fscanf supposed to know whereforename ends and initial begins?Ditto between surname and both integer fields or between day_of_entryand max_wait.patient[20] still does not exist.Why n+1? This insures you will process extraneous data. Fortunately(or un-), the data is initialized since patient is at file scope.Did you mean n-1? That would make sense but the common idiom isi < n, not i <= n-1.Remove del for emailSo how do I avoid the program from skipping when I try to type in themax_wait, in patient[20].max_waitThanksChrisAre we supposed to guess what you are talking about? What did youwant? What actually happened?You should verify that fopen succeeded.This is the wrong test. fscanf returns EOF only if no data wasconverted. You should be proceeding only if the return value is 6.You never check for n overflowing the number of elements in yourarray.You never use limit so why do you care what it''s value is.There is no prototype in scope for this function.On a buffered system, calls to printf that don''t end with a ''\n'' maynot appear before the system waits for input. If you want the inputto appear on the same line as the prompt, you should add fflush(stdout);to insure the buffer is flushed to the stream.Every use of patient[20] invokes undefined behavior. The validsubscripts are 0 through 19 (MAXPATIENTS-1).This will cause problems with any following calls to scanf. To enterthe initial, you will have to press two keys, one for the letter andone for ENTER. This will result in two characters in the inputstream, one for the letter and a ''\n'' for the ENTER. The %c will onlyconsume the letter. The ''\n'' will stay in the stream and terminatethe next scanf prematurely.While it is perfectly legal to pass a struct, the commonrecommendation is to pass a pointer to the struct if the structure islarger that trivial.There is no prototype in scope for this function. You also forgot toprovide the definition of this function.Do you really think this comment contains any useful information?You already read the records into patient back in main. Why are youreading them again?You write the fields of the record with no intervening charactersbetween them. How is your call to fscanf supposed to know whereforename ends and initial begins?Ditto between surname and both integer fields or between day_of_entryand max_wait.patient[20] still does not exist.Why n+1? This insures you will process extraneous data. Fortunately(or un-), the data is initialized since patient is at file scope.Did you mean n-1? That would make sense but the common idiom isi < n, not i <= n-1.Remove del for emailSo how do I avoid the program from skipping when I try to type in themax_wait, in patient[20].max_waitThanksChris 这篇关于怎么了?我不能添加......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 06:05
查看更多