Friday 22 July 2016

Find a Word in a Words List in a File | C++ ifstream Example

Find a Word in a List of Words in a File | C++ ifstream Example

#include <iostream>
#include <fstream>
using namespace std;

int main()
{                                                                        
     int i=0,no_of_words;                  //initialization
     char library[20],input[20];           //declaring strings    
    
     cout << "Enter total no of words: ";  //enter total no. of words
     cin >> no_of_words;

     cout<<"Enter a word to Search: ";     //enter a word to search
     cin>>input;                        

     ifstream fin("words.txt");            //open the file of words   
                                   
     for (i=0;i<50;i++){   //loop that searches the word in the library (file)
    
           fin>>library;
           if (strcmp(library,input)==0) //if word is found
                break;              
     }

     if(i<50)
           cout<<"\nThe word has been found in the library\n";
     else
           cout<<"\nThe word is not in the library\n";             

     return 0;
}

No comments

Post a Comment

Recent Posts