Reputation: 101
I have a binary search function that will search for a word in an array, but before I can search the array I need to know what word to search for. I have written the code to ask the user for input, but the program prints out the request for input but doesn't accept anything from the user. I was thinking it was a buffer issue, as I have an initial scanf in the program that loads all the character strings from an external file and places them in an array. I have tried using fflush after my initial scanf, and I tried rewriting the second one with gets, as pointed out in previous threads. Perhaps I am not implementing it correctly. Here's what I have so far, any tips as to why the second scanf isn't working is appreciated.
#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;
}
Upvotes: 0
Views: 960
Reputation: 753665
Your initial scanf()
in a loop read everything up to EOF, so there's nothing left for the 'What word would you like to search for?' scanf()
to read.
One way around this problem is to read the initial names from a file (fopen()
, fscanf()
, fclose()
— and the file name might be an argument to the program, or a fixed name).
Another way you could try is clearerr(stdin);
before the 'What word' scanf()
. That (clearerr()
) unsets the EOF bit and allows scanf()
to try again. It may work if the input of the program is the terminal; it won't help if the input of the program is coming from a file.
int main(int argc, char **argv)
{
char names[320][30];
char str[30];
int i, j;
char *key;
int numOfWords;
char userWord[30];
Set set1, set2, set3;
FILE *fp;
if (argc != 2)
{
fprintf(stderr, "Usage: %s word-file\n", argv[0]);
exit(1);
}
if ((fp = fopen(argv[1], "r")) == 0)
{
fprintf(stderr, "Usage: %s word-file\n", argv[0]);
exit(1);
}
//scan each char string into array names
for (i = 0; i < 320 && fscanf(fp, "%29s", str) != EOF; i++)
{
strcpy(names[i], str);
}
fclose(fp);
//set number of words in file
numOfWords = i;
This insists that you use ./a.out example.dat
(instead of ./a.out < example.dat
). It will then work more or less as you want it to. Of course, the code for reading the file should be in a function that is passed the file name, the array, and the array size. The 320 in the loop is overflow protection and should be an enumeration enum { MAX_WORDS = 320 };
that's used both in the array declaration and the loop. The 29
is overflow protection; it is hard to parameterize that, but it is one less than the second dimension of the array.
Upvotes: 0
Reputation: 174
Your question and code seemed awfully misaligned at first glance...
scanf()
is the wrong function for reading from your file, you want fscanf()
, or if the file is formatted such that each word is on its own line fgets()
works too (newline stops reading of each string). Likewise, you can use gets()
instead of scanf()
to read user input if the input is just a string followed by return(newline).
Everything you need to know about stdio.h
Upvotes: 0
Reputation: 31394
Your second scanf
doesn't work because your first scanf
never terminates. scanf
won't return EOF
unless the input stream is closed - that would be that the console closes.
Since scanf
returns the number of characters read you should instead make your loop condition scanf(%s, str) != 0
. That will make the loop end as soon as the user hits enter without entering anything.
Upvotes: 4