Thread: word/def question

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    word/def question

    Hey, im creating a "mini-dictionary" for birds, and I have realized that only a certain number of word/defines are allowed. How can I extend the limit? Heres the outline of my code without the several defenitions(There are as of now 38 word/def, but I only showed one):
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    #define maxWord 38
    
    int main(int argc, char *argv[])
    {
    char* word[maxWord];
    char* def[maxWord]; 
    char tmp[100]; 
    char test; 
    char name1[50]; 
    char input; 
    
    word [0] = "";
    def [0] = ""; 
    
    for (int i = 0; i < maxWord; i++)
    {
    if(strcmp(tmp, word[i]) == 0)
    cout << def[i];
    }
    system("PAUSE");
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    The easiest way is to stop programming in C and use C++

    Code:
    #include <string>
    #include <map>
    #include <iostream>
    
    int main(){
        std::map<std::string, std::string> dictionary;
        typedef std::map<std::string, std::string>::iterator dictiter;
    
        dictionary["African Swallow"] = "capable of carrying a coconut";
        dictionary["Flip the Bird"] = "an obscene gesture";
        dictionary["Sparrowhawk"] = "Ged";
    
        for (dictiter i = dictionary.begin(); i != dictionary.end(); ++i)
            std::cout << i->first << " is defined as " << i->second << std::endl;
    }
    Untested, YMMV, no warranty express or implied.
    Last edited by Cat; 12-03-2006 at 12:47 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Cool, I tried it, but I was aiming for something where you give a term, and get back the defenition. Im interested in trying it that way, but I have not done any coding in several years. How can I get the input/output effect? Im actually making this as a christmas present, so any help will be appreciated.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Code:
    #include <string>
    #include <map>
    #include <iostream>
    
    int main(){
        std::map<std::string, std::string> dictionary;
    
        dictionary["African Swallow"] = "capable of carrying a coconut";
        dictionary["Flip the Bird"] = "an obscene gesture";
        dictionary["Sparrowhawk"] = "Ged";
    
        std::string bird = "African Swallow";
        std::cout << "An african swallow is " << dictionary[bird] << std::endl;
    }
    Note:

    1. If the definition doesn't exist it will return an empty string
    2. The key is case sensitive (so "African Swallow" is not the same as "african swallow"). You can get around this by making everything lowercase always.
    Last edited by Cat; 12-03-2006 at 03:44 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Sorry I must be missing something. When I run it, it simply displays the information. heres what I have:
    Code:
    #include <string>
    #include <map>
    #include <iostream>
    
    int main(){
        std::map<std::string, std::string> dictionary;
        typedef std::map<std::string, std::string>::iterator dictiter;
    
        dictionary["African Swallow"] = "capable of carrying a coconut";
        dictionary["Flip the Bird"] = "an obscene gesture";
        dictionary["Sparrowhawk"] = "Ged";
    
        for (dictiter i = dictionary.begin(); i != dictionary.end(); ++i)
            std::cout << i->first << " is defined as " << i->second << std::endl;
    
    
    system("PAUSE");
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Yeah, check the second version of the code, I kept some parts but changed others.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM