아주 간단한 질문입니다. scanf가 첫 번째 while 루프에서 건너 뛴 이유는 무엇입니까? getchar()을 사용하여 시도한 결과가 동일합니다. getchar를 건너 뜁니다.scanf getchar 함수를 건너 뛰었습니다.
너희들이 내가 말하는 것에 대해 이해할 수 없다면 그것을 컴파일 해 볼 수 있고 너희들은 내가 무엇을 요구하는지 이해할 것이다. scanf
입력 버퍼에 '\n'
(endline) 심볼을두고 있기 때문에
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct rec{
int num;
char pref[16];
float point;
struct rec* next;
}rec;
void dataInput(float*,char*);
rec* insertNode(rec*);
int main(){
int i=1;
rec* entr,*pt = NULL;
entr = (rec*) malloc(sizeof(rec));
entr->num = i;
dataInput(&entr->point,entr->pref);
entr->next = NULL;
char key;
i++;
while(1){
printf("Continue ? If YES press 'y',or NO press 'n'\n");
key = getchar();
if(key == 'n')break;
else if(key == 'y'){
if(!pt){
pt = insertNode(entr);
}else{
pt = insertNode(pt);
}
dataInput(&pt->point,pt->pref);
pt->num = i;
i++;
continue;
}else{
printf("Wrong key! Please Press again! \n");
}
}
pt = entr;
while(pt){
printf("num : %d, pref : %s, point: %.1f\n",
pt->num,
pt->pref,
pt->point);
pt = pt->next;
}
getchar();
getchar();
return 0;
}
void dataInput(float* point,char* pref){
printf("Input Point\t : ");
scanf("%f",point);
printf("Input Pref\t : ");
scanf("%s",pref);
}
rec* insertNode(rec* current){
rec* newnode = (rec*)malloc(sizeof(rec));
current->next = newnode;
newnode->next = NULL;
return newnode;
}
예! 그것은 작동! if (key == '\ n') {key = getchar(); } 내가 추가 한 후 . 고맙습니다 – edisonthk