I have this dictionary project that's making my head spin. Am trying to create a dictionary that stores the word and meaning of the word in a text file. Then a user can either searching the meaning of a word or search by alphabet.
I had an idea which is, i wanted to use a function that searches for a specific string in a text file but i dont know how. I hope this is all not too confusing.
please help.
Thanks in advance.

searching a file for a specific string is fairly straight forward. Open the file, read each line and search the lines for desired string. use std::ifstream object for the file, getline() to read each line into a std::string object, the use the string's find() method to see if it contains the desired string.

searching a file for a specific string is fairly straight forward. Open the file, read each line and search the lines for desired string. use std::ifstream object for the file, getline() to read each line into a std::string object, the use the string's find() method to see if it contains the desired string.

What's the syntax for the string's find method ? I've never used it before.

>>What's the syntax for the string's find method
It only has one parameter -- the string you want to look for. It returns either string::npos when the string is not found or the index where it starts.

searching a file for a specific string is fairly straight forward. Open the file, read each line and search the lines for desired string. use std::ifstream object for the file, getline() to read each line into a std::string object, the use the string's find() method to see if it contains the desired string.

Am kind of going nowhere.
For example if this is my text file

anonymous one whose name is not known
anthropology study of mankind
sad unhappy
glad happy
mad crazy
semantic relating to the meaning of words
ageless continuing forever or indefinitely

and the user of the program wants to find the meaning of semantic, how would i do it ?

in your example read the whole line then extract just the first word and compare it. To extract the first word look for space and use substring method.

std::string line = "semantic relating to the meaning of words";
std::string::size_type spot = line.find(' '); // look for a space
std::string word = line.substr(0, spot);
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Dictionary
{  private: char alphabet;
                 string meaning;
                 string word;
               
public:
       
       void getmeaning(string *m);
       void search()
       {
        string word;
        cout<<"enter a word :";
        cin>>word;     
        getmeaning(&word);
       }


}di;

     
void Dictionary::getmeaning(string *p)
{
     
     string a,b;
     ifstream get("dic.txt",ios::in);
     while ( !get.eof())
     {
     get>>a>>b;      
     if (*p==a){
     cout<<a<<"   "<<b;
                }
     }
     }
int main(){
     int ch;
  

     cout<<"======================D=I=C=T=I=O=N=A=R=Y==========================="<<endl;
     cout<<"1.Show meaning"<<endl;
     cout<<"2.Show word"<<endl;
     cout<<"3.Exit"<<endl;
     cin>>ch;
     switch(ch)
     {
      case 1: di.search();

              break;
      case 2: di.getmeaning();
           break;
      case 3 :
           return 0;
      }
     




system("pause");
return 0;
}

Can anyone help me debug this. I keep getting this error.

In function `int main()':
no matching function for call to`Dictionary::getmeaning()'
candidates are: void Dictionary::getmeaning(std::string*)

line 53 is missing the parameter to getmeaning().

Since this is a c++ program you should use references instead of C-style pointers. Makes the program less prone to errors

getmeaning(string& str);
..
void Dictionary::getmeaning(string& p)
{

}

..

line 53 is missing the parameter to getmeaning().

Since this is a c++ program you should use references instead of C-style pointers. Makes the program less prone to errors

getmeaning(string& str);
..
void Dictionary::getmeaning(string& p)
{

}

..

How do i pass by reference if am not going to assign a value to the variable.
for example..

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

int main(void) {
  int a = 10;
  int b = 20;

  swapnum(a, b);

In that case 10 and 20 are assigned to a and b.
In my program am using strings and the user is meant to enter the values rather than them being assigned.
So how do i go about that ?

This is what i did with my code but its wrong.

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

class Dictionary
{  private: char alphabet;
                 string meaning;
                 string word;
               
public:
       
       void getmeaning(string& str);
       void search()
       {
        string word;
        cout<<"enter a word :";
        cin>>word;     
        getmeaning(word);
       }


}di;

     
void Dictionary::getmeaning(string& p)
{
     
     string a,b;
     ifstream get("dic.txt",ios::in);
     while ( !get.eof())
     {
           get>>a>>b;      
           if (p==a){
           cout<<a<<"   "<<b;
                }
           }
     }
int main(){
     int ch;
     string word;
  

     cout<<"======================D=I=C=T=I=O=N=A=R=Y==========================="<<endl;
     cout<<"1.Show meaning"<<endl;
     cout<<"2.Show word"<<endl;
     cout<<"3.Exit"<<endl;
     cin>>ch;
     switch(ch)
     {
      case 1: di.search();

           break;
      case 2: di.getmeaning(word);//problem passing arguments
           break;
      case 3 :
           return 0;
      }
     

system("pause");
return 0;
}
case 2: di.getmeaning(word);//problem passing arguments

Here, you haven't asked the user to type in a word, so your string will be blank

Thanks. Got an exam right now. I'll check the link later.

This article has been dead for over six months. Start a new discussion instead.