Reputation: 31
I made a dictionary. My goal is to have the user enter new words and define them.
I think I have the 'define words' part down, and I have the rest down. I wrote an example of kinda what I am looking for below. I don't want someone to do it for me; I just want to know if there is a way to do this and where I could learn more.
Right now, I am using C++ for Dummies and Sam's Teach Yourself For Teachers.
string newword
string word
cout << "Please enter a word" >> endl;
cin >> word;
if (word == newword)
{
create string <newword>; // Which would make the following
// source code appear without
// actually typing anything new
// into the source code.
}
string newword
string word
string word2 // which would make this happen
cout << "Please enter a word" >> endl;
cin >> word;
if (word == newword)
{
create string <newword>
}
Upvotes: 3
Views: 311
Reputation: 18850
I would use std::map
since, well, it's a dictionary style container. The map
container is perfect for this situation, and you can provide unique keys to match with other data. Since a typical dictionary only has one entry for each word, this is perfect.
A typedef allow us to define a type with a name. It is helpful here here as we don't have to type std::map<std::string, std::string>
over and over. Imagine each time you see Dictionary
, it is replace with std::map<std::string, std::string>
// Map template requires 2 types, Key type and Value type.
// In our case, they are both strings.
typedef std::map<std::string, std::string> Dictionary;
Dictionary myDict;
Then I would ask the user for entries, and then ask them to define their entries.
std::string word;
std::cout << "What word would you like added to the dictionary?" << std::endl;
std::cin >> word;
std::string definition;
std::cout << "Please define the word " << word << std::endl;
std::cin >> definitiion;
Next step would simply insert the word and it's definition into the dictionary. Using the []
operator on a map, we replace any entry that already exists for the supplied key word
. If it doesn't exist already, it will be inserted as a new entry. Note that any previously defined word of the same name will now have a new definition!
myDict[word] = definition;
Running this would produce something similar to:
>> What word would you like added to the dictionary?
>> Map
>> Please define the word Map
>> Helps you find things
Accessing definitions in the map is now trivial:
myDict["Map"]; // Retrieves the string "Helps you find things"
Upvotes: 3
Reputation: 51226
EDIT: My answer only shows you how to build a list of words without definitions. Hopefully it will open some mental doors, but to accomplish your main goal of attaching a definition to each word, you'll want to use a map
instead of a vector
, as Aesthete's answer shows.
What you need is a variable containing a collection of strings. One of the easiest to use, and most commonly used, is a vector:
// At the top of your program
#include <vector>
...
vector<string> words;
...
cout << "Please enter a word" << endl;
cin >> word;
words.push_back(word); // This adds word to the end of the vector.
A vector behaves very much like an array in that if you have a vector called words
, you can access the (i+1)th element using the syntax words[i]
:
cout << "The 3rd word is " << words[2] << endl;
You can replace the 2
above with some other more complicated expression, including one that depends on variables. That allows you to do things like list all the words.
for (int i = 0; i < words.size(); ++i) {
cout << "Word " << (i + 1) << " is " << words[i] << endl;
}
etc.
Upvotes: 1