我正在尝试制作一本字典而我似乎无法搜索该单词是否已存在

时间:2013-12-02 01:13:06

标签: c++ string dictionary fstream

分配是将文字和定义写入文件,并要求用户搜索文件,以便在文字已存在时可以在屏幕上输出。我设法完成了第一部分,但我似乎无法输出已存在的单词的定义。有什么建议吗?

#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;
}  

1 个答案:

答案 0 :(得分:0)

不是答案,但我让它更容易阅读。在searchFile函数中,没有使用line,你的意思是用它做什么吗?如果你用getline读取整行,那么你要搜索的单词永远不会等同于getline返回的字符串,也许尝试用冒号分隔?

#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;
}