0

I need to code an program that finds anagrams of words. When the program starts, it asks the user to enter words into a 'dictionary' which will be searched later on for anagrams of words which again the user will input.

I have stored all the words of the dictionary in a vector called oldDict. The characters in each word are then arranged alphabetically and the new character-sorted words are stored in a vector called newDict, to preserve the original words in oldDict.

The user then enters a word for which anagram(s) must be found in the dictionary. as soon as the word is entered, i again tried to sort the characters of the word alphabetically, in order to then compare it to each element in newDict and find anagrams in this manner.

Below is my code:

ifndef ANAGRAM_H
#define ANAGRAM_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector <string> oldDict;
vector <string> newDict;
int dictSize = 0;

void readDict(void){    // Allows the user to input all the words they would like to     have in the anagram dictionary.
    cout<< "Please enter the number of dictionary entries you wish to create\n";
    cin >> dictSize;
    string word = "";
    cout << "Please enter the dictionary entries, 1 by 1, pushing <enter> after each entry.\n";
    for(int i = 0; i <dictSize; i++){
        cin >> word;
        oldDict.push_back(word);
    }
    newDict = oldDict;
}   

void sortChars(void){   //sorts the letters in each word of the 'dictionary' so that the     letters are in alphabetical order.
    for(int i = 0; i < dictSize; i++){
        std::sort(newDict[i].begin(), newDict[i].end());    
    }
}

void getWords(void){
    int num = 0;
    cout << "Please enter the number of words for which you would like to find anagrams     of:\n";
    cin >> num;
    string word = "";
    for(int i = 0; i < num; i ++){
        cout << "Please enter a word:\n";
        cin>>word;
        std::sort(word.begin(), word.end());    
        for(int i = 0; i < dictSize; i++){
            string word2 = newDict[i];
            bool isAn = isAnagram(word, word2);
            if(isAn == true){
                cout << oldDict[i];
            } else{
            }
        }
    }
}

bool isAnagram(string word1, string word2){

    if(word1.compare(word2) ==0){
        return true;
    } else {
        return false;   
    }
}

void menu(void){
    readDict();
    sortChars();
    getWords();
}
#endif

The process starts in the order() function at the bottom of the code.

When trying to compile the code, i receive the following errors:

In file included from main.cpp:3:0:
./anagram.h: In function ‘void sortChars()’:
./anagram.h:25:3: error: ‘sort’ is not a member of ‘std’
   std::sort(newDict[i].begin(), newDict[i].end()); 
   ^
./anagram.h: In function ‘void getWords()’:
./anagram.h:37:4: error: ‘sort’ is not a member of ‘std’
    std::sort(word.begin(), word.end()); 
    ^
./anagram.h:40:38: error: ‘isAnagram’ was not declared in this scope
     bool isAn = isAnagram(word, word2);

Could someone please help me solve those errors? I really dont understand while 'isAnagram' is giving an error, and also if someone could please explain what 'std::' does and why those two lines of code are creating errors?

Thanks very much

2
  • #include <algorithm> and declared functions before use Commented May 4, 2014 at 13:17
  • Typo-based issue: unlikely to be useful to anyone else. Commented May 4, 2014 at 15:00

1 Answer 1

2

‘sort’ is not a member of ‘std’ Add #include <algorithm>

‘isAnagram’ was not declared in this scope Declare the function before its first use.

Moreover implementation of isAnagram doesn't look correct. You can not plain compare strings. You should sort the strings before comparing them.

bool isAnagram(string word1, string word2){
    std::sort(word1.begin(), word1.end()); // added
    std::sort(word2.begin(), word2.end()); // added
    if(word1.compare(word2) ==0){
        return true;
    } else {
        return false;   
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the response. I'm having trouble understanding what you mean when you say 'declare the function before its first use'? Is it incorrect how i have declared a boolean variable 'isAn' and immediately assigned to it a boolean value, which will be returned by the isAnagram function?
Oh nevermind, i keep forgetting that the order of functions matters in C++. Thanks very much for all your help :)
In case you want to understand declaration vs definition in C and C++, I would suggest googling highlighted term and go through a few links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.