Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:39:21+00:00 2026-05-31T19:39:21+00:00

I have a binary search function that will search for a word in an

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:39:22+00:00Added an answer on May 31, 2026 at 7:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1-)For sorted array I have used Binary Search. We know that the worst case
I have a modified version of binary search that takes in an array in
I have a binary search function I am passing a pointer character array, the
I have a PHP script that builds a binary search tree over a rather
I have a binary file that I would like to regex search/replace hex bytes
I have a scenario where I need to search from many binary files (using
I am writing a delete member function for a Binary Search Tree. I have
How can I check that my binary tree doens't contain duplicates? Do you have
How can I implement a function to delete an element in a binary search
We have a function in our code which isn't being called, but should be.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.