为什么scanf函数无法结束输入
scanf 函数无法结束输入,一直可以输入,按回车时会换行,但依然可以输入,这是怎么回事呢
[解决办法]
在scanf后面接着用getchar来接收回车键试试。
[解决办法]
scanf("%s %s %s %s",new->name,new->corp,new->phone,new->address);
==>
scanf("%s%s%s%s",new->name,new->corp,new->phone,new->address);
先像上面那样修改再说.
什么编辑器?怎么把行号一起复制过来了??
[解决办法]
改成这样了.
似乎是能够跳出去的..至于其他逻辑问题,你自己改吧.
- C/C++ code
#include <stdio.h>#include <string.h>#include <stdlib.h>struct Touch{ char name[20]; char corp[20]; //work char phone[20]; char address[50]; struct Touch* next;};typedef struct Touch AddressInfo;#define new newpAddressInfo* Create(){ int num, i; AddressInfo* head, *p, *new; head = (AddressInfo *)malloc(sizeof(AddressInfo)); if(!head) { printf("Fail to assign memory !\n"); exit(1); } new = p = head; printf("Input the first person data :\n"); scanf("%s %s %s %s", head->name, head->corp, head->phone, head->address); head->next = NULL; printf("Input the number you want to save :\n"); scanf("%d", &num); for(i = 0; i < num; i++) { new = (AddressInfo *)malloc(sizeof(AddressInfo)); if(!new) { printf("Fail to assign memory !\n"); exit(1); } p->next = new; //combine printf("Input the %d person's data :\n", i + 1); scanf("%s %s %s %s", new->name, new->corp, new->phone, new->address); p = new; p->next = NULL; } p = head; while(p != NULL) { printf("%s\t%s\t%s\t%s\n", p->name, p->corp, p->phone, p->address); p = p->next; } printf("\n"); return head;}void Save(AddressInfo* head){ AddressInfo* p; FILE* fp; if((fp = fopen("Address.txt", "wb")) == NULL) { printf("File open error!\n"); exit(0); } for(p = head; p->next != NULL; p = p->next) { if(1 != fwrite(p, sizeof(AddressInfo), 1, fp)) break; } if(fclose(fp)) { printf("Can not close the file\n"); exit(0); }}AddressInfo* load(){ AddressInfo* p, *new, *head; p = new = head = NULL; FILE* fp; if((fp = fopen("Address.txt", "rb")) == NULL) { printf("can not open file\n"); exit(1); } printf("\n -----Loading file!-----\n"); head = (AddressInfo *)malloc(sizeof(AddressInfo)); if(!head) { printf("out of memory!\n"); return NULL; p = head; while(!feof(fp)) { if(1 != fread(p, sizeof(AddressInfo), 1, fp)) break; new = (AddressInfo *)malloc(sizeof(AddressInfo)); if(!new) { printf("Error to assign memory!\n"); return head; } p->next = new; p = new; } p->next = NULL; fclose(fp); printf("Load success !\n"); return head; }}AddressInfo* Addition(AddressInfo* head, int n){ puts(__FUNCTION__); int i; AddressInfo* p, *new; p = head; while(p->next != NULL) //Find the last node { p = p->next; } printf("%d\n", n); for(i = 0; i < n; i++) { new = (AddressInfo *)malloc(sizeof(AddressInfo)); if(!new) { printf("Fail to assign memory !\n"); exit(1); } p->next = new; scanf("%s%s%s%s", new->name, new->corp, new->phone, new->address); p = new; p->next = NULL; } p = head; while(p->next != NULL) { printf("%s\t%s\t%s\t%s\n", p->name, p->corp, p->phone, p->address); } printf("\n"); puts(__FUNCTION__); return head;}int main(){ char c; int n; AddressInfo* head, *p; head = Create(); //create a Touch_person linkes list Save(head); printf("Please choise :\nA: Add a person in the end\nD: Delete a person\nF: Find a person\nI: Insert a person\nP: Print all persons\nE: exit\n"); while(1) { c = getchar(); if(c == 'a') { head = load(); printf("Input the number of u want to add :\n"); scanf("%d", &n); head = Addition(head, n); Save(head); } } return 0;}
[解决办法]
PK,解决了。改了1个地方,把new改成了pnew。
- C/C++ code
#include <stdio.h>#include <string.h>#include <stdlib.h>struct Touch{ char name[20]; char corp[20]; //work char phone[20]; char address[50]; struct Touch *next;};typedef struct Touch AddressInfo;AddressInfo *Create() //创建链表{ int num,i; AddressInfo *head,*p,*pnew; head=(AddressInfo *)malloc(sizeof(AddressInfo)); if(!head) { printf("Fail to assign memory !\n"); exit(1); } pnew=p=head; printf("Input the first person data :\n"); scanf(" %s %s %s %s",head->name,head->corp,head->phone,head->address); head->next=NULL; printf("Input the number you want to save :\n"); scanf("%d",&num); for(i=0;i<num;i++) { pnew=(AddressInfo *)malloc(sizeof(AddressInfo)); if(!pnew) { printf("Fail to assign memory !\n"); exit(1); } p->next=pnew; //combine printf("Input the %d person's data :\n",i+1); scanf("%s %s %s %s",pnew->name,pnew->corp,pnew->phone,pnew->address); p=pnew; p->next=NULL; } p=head; while(p!=NULL) { printf("%s\t%s\t%s\t%s\n",p->name,p->corp,p->phone,p->address); p=p->next; } printf("\n"); return head;}AddressInfo *Addition(AddressInfo *head,int n){ int i; AddressInfo *p,*pnew; p=head; while(p->next!=NULL) //Find the last node { p=p->next; } for(i=0;i<n;i++) { pnew=(AddressInfo *)malloc(sizeof(AddressInfo)); if(!pnew) { printf("Fail to assign memory !\n"); exit(1); } p->next=pnew; scanf("%s %s %s %s",pnew->name,pnew->corp,pnew->phone,pnew->address); p=pnew; p->next=NULL; } p=head; while(p->next!=NULL) { printf("%s\t%s\t%s\t%s\n",p->name,p->corp,p->phone,p->address); } printf("\n"); return head;}AddressInfo *Delete(AddressInfo *head,AddressInfo *d) { AddressInfo *p,*q; p=head; q=d->next; d->next=q->next; free(q); while(p->next!=NULL) { printf("%s\t%s\t%s\t%s\n",p->name,p->corp,p->phone,p->address); p=p->next; } printf("\n"); return head;}void Find(AddressInfo *f){ printf("%s\t%s\t%s\t%s\n",f->name,f->corp,f->phone,f->address); printf("\n");}void Save(AddressInfo *head) //把链表保存到二进制文件中{ AddressInfo *p; FILE *fp; if((fp=fopen("Address.txt","wb"))==NULL) { printf("File open error!\n"); exit(0); } for(p=head;p->next!=NULL;p=p->next) { if(1!=fwrite(p,sizeof(AddressInfo),1,fp)) break; } if(fclose(fp)) { printf("Can not close the file\n"); exit(0); }}AddressInfo *load() //把文件中的信息加载到链表中{ AddressInfo *p,*pnew,*head; FILE *fp; p = pnew = head = NULL; if((fp=fopen("Address.txt","rb"))==NULL) { printf("can not open file\n"); exit(1); } printf("\n -----Loading file!-----\n"); head=(AddressInfo *)malloc(sizeof(AddressInfo)); if(!head) { printf("out of memory!\n"); return NULL; } p=head; while(!feof(fp)) { if(1!=fread(p,sizeof(AddressInfo),1,fp)) break; pnew=(AddressInfo *)malloc(sizeof(AddressInfo)); if(!pnew) { printf("Error to assign memory!\n"); return head; } p->next=pnew; p=pnew; } p->next=NULL; fclose(fp); printf("Load success !\n"); return head;}int main(void){ char c; int n; AddressInfo *head,*p; head=Create(); //create a Touch_person linkes list Save(head); while(1) { printf("Please choise :\nA: Add a person in the end\n"); c=getchar(); if(c=='a') { head=load(); printf("Input the number of u want to add :\n"); scanf("%d",&n); head=Addition(head,n); Save(head); } return 0; }}