Search code examples
c++stringdictionaryfstream

I am trying to make a dictionary and I can't seem to search to see if the word already exists


The assignment is to write words and a definition to a file and ask the user to search through the file so it can output on the screen if the word already exists. I managed to get the first part done but I can't seem to output the definition of an already existing word. Any suggestions?

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

void searchFile(string word){
     ifstream ifile;
     ofstream ofile;
     ofile.open("dictionary.txt", ios::app);
     string line;

while(ofile.is_open()){
                         ifile >> line;
                         if(word == line){
                         getline(cin, line);
                         cout << line << endl;
                         ofile.close();
                         }

                         else{
                         cout << "This definition does not exist." << endl;
                         ofile.close();
                         }
                         }
}

int main(){
     char ans;
     ifstream inputFile;
     ofstream outputFile;
     string search;


outputFile.open("dictionary.txt", ios::app);

while(outputFile.is_open()){
     string word;
     string def;

     cout << "Please enter the word and press enter." << endl;
     cin >> word;
     cin.ignore(1000,'\n');
     outputFile << word;
     outputFile << " : ";

     cout << "Please enter the definition of this word." << endl;
     getline(cin, def);
     outputFile << def << endl;

    cout << "Would you like to close the dictionary? y/n" << endl;
    cin >> ans;
    if(ans == 'y'){
        outputFile.close();
    }
}
    cout << "Would you like to search the dictionary? y/n" << endl;
    cin >> ans;
    if(ans == 'y'){
        cout << "What word would you like to search for?" << endl;
        cin >> search;
        searchFile(search);
    }


system("pause");
return 0;
}  

Solution

  • Not an answer, but I made it a little easier to read. In the searchFile function, line is not being used, did you mean to use it for something? If you're reading an entire line in with getline, then the word you are searching for will never be equivalent to the string returned from getline, maybe try delimiting with a colon?

    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    
    void searchFile(string word)
    {
         ifstream ifile;
         ofstream ofile;
         ofile.open("dictionary.txt", ios::app);
         string line;
         string search;
    
        while(ofile.is_open())
        {
            getline(ifile, search);
            ifile >> search;
            if(word == search)
            {
                getline(ifile, search);
                cout << search << endl;
                ofile.close();
            }
            else
            {
                cout << "This definition does not exist." << endl;
                ofile.close();
            }
        }
    }
    
    int main()
    {
        char ans;
        ifstream inputFile;
        ofstream outputFile;
        string search;
    
    
        outputFile.open("dictionary.txt", ios::app);
    
        while(outputFile.is_open())
        {
            string word;
            string def;
    
            cout << "Please enter the word and press enter." << endl;
            cin >> word;
            cin.ignore(1000,'\n');
            outputFile << word;
            outputFile << ": ";
    
            cout << "Please enter the definition of this word." << endl;
            getline(cin, def);
            outputFile << def << endl;
    
            cout << "Would you like to close the dictionary? y/n" << endl;
            cin >> ans;
            if(ans == 'y')
            {
                outputFile.close();
            }
        }
    
        cout << "Would you like to search the dictionary? y/n" << endl;
        cin >> ans;
    
        if(ans == 'y')
        {
            cout << "What word would you like to search for?" << endl;
            cin >> search;
            searchFile(search);
        }
    
        system("pause")
        return 0;
    }