Hi I have my first assignment with classes and creating your own header file.
We were given a file with words and a definition next to the word. That should be read into the program. Each word and definition pair should be put in one spot in the vector. So if there are 10 words then there should be 10 spots for the pair.
Part 1 of the assignment we need to get a word from the user and then output the definition.
Part 2 is we need to sort the file alphabetically
I am having trouble for the part where you read the file and place it into the vector. And for the functions get_word(), search(), and sort().
#include <iostream>
#include "functions.h"
dictionary::dictionary(string wordD, string defD): word(wordD), def(defD){};
//alphabeltical order
void sort(){
//code
}
dictionary::get_word(){
cout <<"Please enter a word: \n";
cin >>word;
search(word);
}
void search(string word){ //not sure if this would work
for (int i = 0; i > wd.size(); i++){
if (wd[i].get_word == word){
cout <<"The definition is " <<wd[i].get_word <<endl;
}
else {
cout <<"The word is not in the dictionary\n";
}
}
#include <iostream>
#include <vector>
#include <fstream>
#include "functions.h"
#include <stdlib.h>
#include <string>
#include <cstdlib>
usingnamespace std;
int main (int argc, char* argv[]){
dictionary temp;
vector <dictionary> wd;
ifstream fin;
fin.open("Words_list.txt");
if(fin.fail()){
cout <<"error.\n";
}
else{
string word, def; //do'nt know if I can decalare these bec my private variables in class.
while (!fin.eof()){ //
fin >> word;//the word is read in here
getline(fin, def);//def is read in here
dictionary temp(word, def); //wanted to put the word and def in here
wd.push_back(temp); //then put it in the vector
}
fin.close();
}
//get_word();
//sort();
system ("Pause");
return 0;
}
> "no matching function for call to `dictionary::dictionary()'"
You have defined a custom constructor, so the default no longer exists. If you need one, then you need to define it.
However, ¿do you need a default constructor? In line 11 you do dictionary temp; and never use that variable.
> 'class std::vector<dictionary, std::allocator<dictionary> >' has no member named 'pushback' "
It is called push_back