Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
okay let say i made a file of input which contains word and its meaning. so i allow user to ask the word that their want to search. for example user want to search word 'time', so word 'time' is in the file of input and its meaning will be display to the terminal.
but why my output shows that the word 'time' is not found while word 'time' contains in the input file.

What I have tried:

C++
case 2: {
    string searchWord;
    cout << "Enter the word to search: ";
    cin >> searchWord;

    bool found = false;

    string word, meaning;
    while (getline(inFile, word) && getline(inFile, meaning)) {
        if (searchWord == word) {
            char firstAlphabet = searchWord.at(0);
            cout << "Word " << searchWord << " found in the dictionary.\n\n";
            cout << lineNum++ << "\t" << firstAlphabet << "\t" << searchWord << "\t" << meaning << endl;
            found = true;
            break;
        }
    }

    if (!found) {
        cout << "Word " << searchWord << " not found in the dictionary.\n";
    }

    cout << "\nDone...\n";
    break;
}
Posted
Updated 20-Dec-23 20:46pm
v2

The following code
C++
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    string searchWord;
    cout << "Enter the word to search: ";
    cin >> searchWord;

    bool found = false;

    ifstream inFile("dict.txt");

    string word, meaning;
    size_t itemNum = 0;
    while (getline(inFile, word) && getline(inFile, meaning))
    {
        if (searchWord == word) {
            char firstAlphabet = searchWord.at(0);
            cout << "Word " << searchWord << " found in the dictionary.\n\n";
            cout << (itemNum+1) << "\t" << firstAlphabet << "\t" << searchWord << "\t" << meaning << endl;
            found = true;
            break;
        }
      ++itemNum;
    }

    if (!found) {
        cout << "Word " << searchWord << " not found in the dictionary.\n";
    }

    cout << "\nDone...\n";
}
works on my Linux box, provided the input file is properly structured (that is a whole line for any word followed by a whole line for the word definition; for example, my dict.txt is
money
a current medium of exchange in the form of coins and banknotes
time
the indefinite continued progress of existence and events in the past, present, and future regarded as a whole
universe
all existing matter and space considered as a whole
 
Share this answer
 
Comments
Andre Oosthuizen 21-Dec-23 7:44am    
+5
CPallini 21-Dec-23 7:57am    
Thank you.
The source code above is missing <string> for getline. I have tested the code under Windows with VS and it seems to work if you add copy the input file from CPallini to it. I would suggest a little more handling of possible errors and the use of the debugger.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900