I'm relatively new to data structures and using push_back. I'm trying to build a small dictionary program that assumes that there is no words (and definitions) in the dictionary when you first launch it. What I then need to do is ask for a word then definition to start building the library of words and definitions.

Only part I'm stuck on is how to push back the words and definitions the user enters into their respective strings. If someone can point me on the right direction, much appreciated :)

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

struct Term {

	string word;
	string definition;


struct Dictionary {

	vector<Term> term;


void programLoop() {

	//variables to use for program
	string userInputString = "";
	string searchString = "Search";
	string helpString = "Help";
	string exitString = "Exit";


	 do {
		 cin >> userInputString; //get string input

		 if ( userInputString.compare(searchString) == 0 ) { //search for word

		Dictionary myDictionary;

		string userWord;
		string userDefinition;

		bool matchFound = false;

		if ( myDictionary.term.size() == 0 ) {//first case
		cout << "Please enter a word: ";
		getline (cin,userWord);
		cout << userWord << "\n";
		myDictionary.term.push_back(userWord); //trying to figure out this part but stuck

		cout << "Please enter the definition of that word: ";
		getline (cin, userDefinition);
		cout << userDefinition << "\n";


// I cut some functions out and some other code since not really relevant to the issue. If someone wants to see full code let me know.

} //end main
vector<Term> term;
string userWord;

See red. These need to match. You have a vector of Term. Push a Term, not a string. Try this.

Term aTerm;
aTerm.word = userWord;
aTerm.definition = userDefinition;

Now all the types match and the compiler should be happy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.