The following code
#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