To recognize words

Hello.
I am a beginner and I am about to make a program and I really need your help!

What the program should do: While the program is running, I type in a word, for example "computer". If the the program recognizes he word, it should say that it does.

I have approximately 400 words that the program should recognize.
I have tried different methods with if-statements, but the problem is that they can´t handle whole words.

I hope you understand my problem, and I would appreciate if you could help me!
post a sample code
How are the words stored? As a std::string or as a char array?
At the moment they are stored as a char array.

I can´t post a sample code, cause I have not come any further than writing down my words. Then my slow brain can´t figure out anything clever to do. I know this should be a pretty easy thing to make. Any ideas?
Last edited on
First, I would consider placing your dictionary of words into a text file and reading it by the program rather than having a hard-coded array of strings. You could, for example, read these words into an STL set. STL set containers are sorted and have efficient searching, so it should be convenient to see if a particular word exists in the set.
If you want to compare C strings -char arrays- use strcmp http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
You'll need a loop to check all the words

If you choose the C++ way ( that suggested by moorecm ), which is much easier, you can go for the standard algorithm count: http://www.cplusplus.com/reference/algorithm/count/ which works with any container
Thanks for your replies! I think I will be able to make the program now...
I was dead wrong; I am not able to make it yet.

I tried to make it the strcmp way, because I know nothing about containers, vectors and stuff.
How do I compare more than two strings? Is there any easy way to compare a string with many others? You talked about a loop, but how should I loop?

Sorry to ask all these questions, but I really need this program...
You could store your 400 words in a string type array. You would need to specify the max number of characters which each array element will accept (I think). You could then sort the array so they are in lexicographic order as in a dictionary.
Invite the user to enter a word of less than (MAX) characters.
Then loop through the array and if word== array[i] cout<<"the word id recognized"; else cout<< "the word is not recognized";
Not too efficient but a start.
Topic archived. No new replies allowed.