배열의 단어를 검색하는 바이너리 검색 기능이 있습니다. 그러나 배열을 검색하기 전에 검색 할 단어를 알아야합니다. 사용자에게 입력을 요청하는 코드를 작성했지만 프로그램은 입력 요청을 인쇄하지만 사용자로부터 아무 것도받지 않습니다. 프로그램에서 초기 scanf를 사용하여 외부 파일의 모든 문자열을로드하고 배열에 배치하는 것이 버퍼 문제라고 생각했습니다. 나는 내 초기 scanf 후 fflush를 사용하여 시도하고, 이전 스레드에서 지적한 바와 같이 gets로 두 번째 재 작성을 시도했다. 아마 나는 그것을 올바르게 구현하지 않을 것입니다. 여기에 내가 지금까지 무엇을, 두 번째 scanf 작동하지 않는 이유에 대한 조언을 주셔서 감사합니다.Scanf가 두 번째 문자 문자열을 허용하지 않음
#include "set.h"
#include "sortAndSearch.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
char names[320][30];
char str[30];
int i, j;
char *key;
int numOfWords;
char userWord[30];
Set set1, set2, set3;
//scan each char string into array names
for(i=0; scanf("%s", str) != EOF; i++){
strcpy(names[i], str);
}
//set number of words in file
numOfWords = i;
//sort names array
//bubbleSort(names, numOfWords);
//print out names, sorted
//for(i=0; i<numOfWords; i++){
// printf("%s\n", names[i]);
//}
printf("What word would you like to search for? ");
scanf("%s", userWord);
//addName2Set(set1, userWord);
return 0;
}
당신이 입력의 * 라인 *을 읽으려면 라인을 읽는 기능을 사용합니다. 그렇지 않으면 단어를 읽고 선 끝을 떠납니다. –