collapse collapse

* Who's Online

  • Dot Guests: 119
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: Dictionary (I fail)  (Read 3673 times)

0 Members and 1 Guest are viewing this topic.

Offline Sinister

  • - Secret Permissions Level 1 -
  • Sr. Member
  • ****
  • Posts: 383
  • Reputation 619
  • Gender: Male
    • View Profile
Dictionary (I fail)
« on: August 10, 2009, 05:02:54 pm »
I'm a failure.

Just make me a dictionary with C++. As simple as possible. Give it a menu in which you can search, add term, delete term, and maybe list in order.

Put comments in it so I can learn from it.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Dictionary (I fail)
« Reply #1 on: August 10, 2009, 05:47:08 pm »
Lol. You made me laugh. Does it need a GUI?
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline Sinister

  • - Secret Permissions Level 1 -
  • Sr. Member
  • ****
  • Posts: 383
  • Reputation 619
  • Gender: Male
    • View Profile
Re: Dictionary (I fail)
« Reply #2 on: August 10, 2009, 05:52:01 pm »
Lol. You made me laugh. Does it need a GUI?

No. Just make it-
actually.

I might need to learn a thing or two about GUI and C++.
So yes. GUI.

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: Dictionary (I fail)
« Reply #3 on: August 10, 2009, 06:51:11 pm »
I'm a failure.

Just make me a dictionary with C++. As simple as possible. Give it a menu in which you can search, add term, delete term, and maybe list in order.

Put comments in it so I can learn from it.
I don't understand, the post you made only had simple mistakes...why are you calling it a failure?
Just because you mess up syntax doesn't mean its a failure.
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Dictionary (I fail)
« Reply #4 on: August 10, 2009, 06:57:48 pm »
Try and repair your program. If you still can't, ill lend you a hand. I intend to post the source for Nathans Archiver, it uses a GUI, a  quick one, but its a GUI non the less.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline NightSky

  • Regular Members
  • Hero Member
  • *
  • Posts: 711
  • Reputation 803
  • Gender: Male
  • Too Good.
    • View Profile
Re: Dictionary (I fail)
« Reply #5 on: August 10, 2009, 08:13:42 pm »
Lol

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: Dictionary (I fail)
« Reply #6 on: November 01, 2009, 02:59:41 pm »
Here's a simple one. It doesn't have a GUI though.. I've never done Windows GUI programming, and don't intend to either.

Edit: oops, forgot the delete functionality.... Added!
Edit: Added loading from and saving to a file. Not too many comments because the code is quite self-explanatory.

Code: C++
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include <cstdio>
  7.  
  8. #define DICT_FILE "dictionary.txt"
  9.  
  10. using namespace std;
  11.  
  12. map<string, string> dict; /* our dictionary array */
  13.  
  14. /* this loads a list of term|definition entries from DICT_FILE */
  15. int loadDefinitions(void)
  16. {
  17.     int count;
  18.     string line;
  19.     ifstream f;
  20.     f.open(DICT_FILE, ios::in);
  21.  
  22.     if(!f.is_open())
  23.         return 0;
  24.  
  25.     while(!f.eof())
  26.     {
  27.         string term, def;
  28.         int offset;
  29.         getline(f, line);
  30.  
  31.         offset = line.find("|");
  32.         if(offset > 0)
  33.         {
  34.             term = line.substr(0,offset);
  35.             def = line.substr(offset+1);
  36.    
  37.             if(term.length() && def.length())
  38.             {
  39.                 ++count;
  40.                 dict[term] = def;
  41.             }
  42.         }
  43.     }
  44.     f.close();
  45.     return count;
  46. }
  47.  
  48. /* this saves all currently loaded definitions into DICT_FILE */
  49. int saveDefinitions(void)
  50. {
  51.     int count;
  52.     ofstream f;
  53.     f.open(DICT_FILE, ios::out | ios::trunc);
  54.  
  55.     if(!f.is_open())
  56.         return 0;
  57.  
  58.     for(map<string, string>::iterator i = dict.begin(); i != dict.end(); i++)
  59.     {
  60.         ++count;
  61.         f << i->first << "|" << i->second << endl;
  62.     }
  63.  
  64.     return count;
  65. }
  66.  
  67. /* this function finds all definitions matching the given term */
  68. vector< pair<string, string> > getDefinitions(string term)
  69. {
  70.     /* this will store the results of our search */
  71.     vector< pair<string, string> > results;
  72.  
  73.     /* looping through the dictionary */
  74.     for(map<string, string>::iterator i = dict.begin(); i != dict.end(); i++)
  75.     {
  76.         /* if a term is found.... */
  77.         if(i->first.find(term) != string::npos)
  78.         {
  79.             /* ...push the term and definition into results as a tuple */
  80.             results.push_back(make_pair(i->first, i->second));
  81.         }
  82.     }
  83.  
  84.     return results;
  85. }
  86.  
  87. /* this deletes a definition. Unlike getDefinitions(), this term must be an exact match. */
  88. void deleteDefinition(string term)
  89. {
  90.     for(map<string, string>::iterator i = dict.begin(); i != dict.end(); i++)
  91.     {
  92.         if(i->first == term)
  93.         {
  94.             dict.erase(term);
  95.             return;
  96.         }
  97.     }
  98. }
  99.  
  100. /* this adds new definitions to our dictionary */
  101. void addDefinition(string term, string definition)
  102. {
  103.     dict[term] = definition;
  104. }
  105.  
  106. /* this function prints out a vector of string tuples (i.e. a list of term-definition pairs) */
  107. void printResults(vector< pair<string, string> > results)
  108. {
  109.     cout << "\n\n";
  110.  
  111.     /* looping through the results */
  112.     for(vector< pair<string, string> >::iterator i = results.begin(); i != results.end(); i++)
  113.     {
  114.         /* and adding some formatting magic to make it look nice. */
  115.         printf("%-20s => %s\n", i->first.c_str(), i->second.c_str());
  116.     }
  117. }
  118.  
  119. /* this function prints out the given string and asks for a input */
  120. string prompt(string input)
  121. {
  122.     string tmp;
  123.     cout << input;
  124.     getline(cin, tmp);
  125.     return tmp;
  126. }
  127.  
  128. int main(void)
  129. {
  130.     int choice;
  131.     int defs;
  132.  
  133.     cout << "Loading definitions from " << DICT_FILE << "...";
  134.     defs = loadDefinitions();
  135.     cout << defs << " definitions loaded.\n";
  136.  
  137.     do {
  138.         cout << "\n0: exit\n1: add new definition\n2: search for a definition\n" <<
  139.                 "3: list all terms\n4: delete a definition\n> ";
  140.         cin >> choice;
  141.  
  142.         /* we need cin.ignore() here for getline to work later on... */
  143.         cin.ignore();
  144.  
  145.         switch(choice)
  146.         {
  147.             case 1:
  148.             {
  149.                 string term = prompt("Enter the term to define: ");
  150.                 string def = prompt("Enter definition for " + term + ": ");
  151.                 addDefinition(term, def);
  152.                 break;
  153.             }
  154.             case 2:
  155.             {
  156.                 printResults(getDefinitions( prompt("Enter a search term: ") ));
  157.                 break;
  158.             }
  159.             case 3:
  160.             {
  161.                 /* "" will match any string, so this will list everything. */
  162.                 printResults(getDefinitions(""));
  163.                 break;
  164.             }
  165.             case 4:
  166.             {
  167.                 deleteDefinition(prompt("Enter the name of the term EXACTLY: "));
  168.                 break;
  169.             }
  170.             default: break;
  171.         }
  172.     } while (choice != 0);
  173.  
  174.     cout << "Saving definitions...";
  175.     if((defs = saveDefinitions()) == 0)
  176.         cout << "Couldn't save definitions.\n";
  177.     else
  178.         cout << defs << " definitions saved.\n";
  179.    
  180.     return 0;
  181. }
  182.  
« Last Edit: November 01, 2009, 03:51:24 pm by jho »
"I'm sorry, I only accept criticism in the form of sed expressions."

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Dictionary (I fail)
« Reply #7 on: November 01, 2009, 03:15:55 pm »
Looks good, if only you had a premade dictionary list, and if only it saved the list so you did not have to readd everything each time you restart the app.
I got to brush up on my vectors. Your first function took me a while to decipher.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: Dictionary (I fail)
« Reply #8 on: November 01, 2009, 03:52:16 pm »
Looks good, if only you had a premade dictionary list, and if only it saved the list so you did not have to readd everything each time you restart the app.
I got to brush up on my vectors. Your first function took me a while to decipher.
How about now? :P
"I'm sorry, I only accept criticism in the form of sed expressions."

Offline jho

  • The flamboyant FLOSS fanatic
  • Sr. Member
  • *
  • Posts: 352
  • Reputation 1199
  • Gender: Male
    • View Profile
    • http://hajotus.net
Re: Dictionary (I fail)
« Reply #9 on: November 01, 2009, 05:19:33 pm »
Oh by the way, here's the same thing written in ruby.... It's three times shorter, and a lot cleaner.. :P

Code: Ruby
  1. DICT_FILE = "dictionary.txt"
  2. $dict = Hash.new
  3.  
  4. def load_dict
  5.     return 0 unless File::exists?(DICT_FILE)
  6.     IO.foreach(DICT_FILE) do |line|
  7.         a = line.chomp.split('|', 2)
  8.         $dict[a[0]] = a[1] unless a.empty?
  9.     end
  10.     $dict.length
  11. end
  12.  
  13. def save_dict
  14.     File.open(DICT_FILE, "w") do |file|
  15.         $dict.each_pair do |a,b|
  16.             file.write "#{a}|#{b}\n"
  17.         end
  18.     end.length
  19. end
  20.  
  21. def get_definition(term)
  22.     $dict.select { |a,b| a.index(term) }.each do |a,b|
  23.         printf "%-20s => %s\n", a, b
  24.     end
  25. end
  26.  
  27. def prompt(str)
  28.     print str
  29.     gets.chomp
  30. end
  31.  
  32. print "Loading definitions from #{DICT_FILE}..."
  33. puts "#{load_dict} definitions loaded."
  34. choice = 0
  35. begin
  36.     choice = prompt("\n0: exit\n1: add new definition\n2: search for a definition\n" +
  37.                     "3: list all terms\n4: delete a definition\n> ").to_i
  38.  
  39.     case choice
  40.         when 1 then lambda do term = prompt "Enter the term to define: "
  41.                               definition = prompt "Enter definition for #{term}: "
  42.                               $dict[term] = definition
  43.                 end.call
  44.         when 2 then get_definition(prompt "Enter a search term: ")
  45.         when 3 then get_definition ""
  46.         when 4 then lambda do term = prompt "Enter the term to be deleted: "
  47.                               $dict.delete(term) if $dict.key?(term)
  48.                 end.call
  49.     end
  50. end while choice != 0
  51. if not $dict.empty?
  52.     print "Saving definitions..."
  53.     defs = save_dict
  54.     if defs == 0
  55.         puts "Couldn't save definitions."
  56.     else
  57.         puts "#{defs} definitions saved."
  58.     end
  59. end
  60.  
"I'm sorry, I only accept criticism in the form of sed expressions."

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]