私は配列内の単語を検索するバイナリ検索機能を持っていますが、配列を検索する前に検索する単語を知る必要があります。ユーザーに入力を求めるコードを書いていますが、プログラムは入力要求を出力しますが、ユーザーから何も受け付けません。私はそれがバッファの問題だと思っていました。私は、プログラム内に初期のscanfがあり、外部ファイルからすべての文字列を読み込んで配列に配置しています。私は最初のscanfの後にfflushを使ってみました。そして、前のスレッドで指摘されているように、getsで2番目のものを書き直そうとしました。おそらく私はそれを正しく実装していないでしょう。ここで私はこれまで何をしているのか、2番目のscanfがうまくいかない理由についてのヒントを感謝します。Scanfが2番目の文字列を受け付けません
#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;
}
あなたが入力の*ライン*を読みたい場合は、行を読み込み、関数を使用します。さもなければ、あなたは単語を読んでいて、行末を残しています。 –