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
#include <algorithm>and declared functions before use