我有一個二進制搜索功能,這將在數組中的單詞搜索,但在此之前我可以搜索數組我需要知道搜索什麼詞。我已經編寫了代碼,要求用戶輸入,但程序打印出輸入請求,但不接受用戶的任何內容。我認爲這是一個緩衝區問題,因爲我在程序中有一個初始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;
}
如果你想讀取輸入的*線*,使用函數讀取一行。否則,您正在閱讀該單詞並將行結束。 – 2012-03-26 01:58:05