[RESOLVED] c++ dictionary
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 46

Thread: [RESOLVED] c++ dictionary

  1. #1
    Join Date
    May 2017
    Posts
    175

    [RESOLVED] c++ dictionary

    I have a total new concept I want to use . I never used this before . I want to add a dictionary to my c++ program I want it to be able to include it in my program to use it to search for words in 2d arrays ( I heard about 8 direction search ) . can you help me ?? thx alot
    Last edited by david16; July 9th, 2017 at 01:31 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    To search for words, you first need a file containing the words. There are several available on the internet that can be downloaded. eg Moby http://icon.shef.ac.uk/Moby/mwords.html

    Once you have a file containing the words, then you need to consider how you want to access them and the appropriate container to use to store them. Then you need to consider the 2d array search.

    Consider
    https://stackoverflow.com/questions/...all-directions
    http://www.cplusplus.com/forum/beginner/62624/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  3. #3
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    Thx I will have a look on these and I will get back to you in case I didnt understand something

  4. #4
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    Actually I never included a library in a program I guess I should first I will have to extract that and add them to a word document right ??
    Last edited by david16; July 10th, 2017 at 08:40 AM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    Quote Originally Posted by david16 View Post
    Actually I never included a library in a program I guess I should first I will have to extract that and add them to a word document right ??
    The link in post #2 is for an example of a downloadable English dictionary. There are several/many available from the internet. Another one may be more suitable for your needs. You'll need to do some research to find which one is the best for you. Once you have found one that is suitable, then you can see about how it can best be used.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  6. #6
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    that dictionary is fine I think if I want to search for english words my 2d arrays . But after I extract it What should I do with the extracted files . I mean I don't know how to include it in my prog in visual studio if its this or another one So can you help me with this ?? thx
    Last edited by david16; July 10th, 2017 at 11:17 AM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    What should I do with the extracted files
    If these are text file(s) and if they are small enough so that the words can be held in memory, then you probably want to iterate the file(s) and store the words in something like a set.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  8. #8
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    They are of different type but I can't open the files with notepad the problem I have is where to store them so I can #include them in my program to use it I'm working on that
    Last edited by david16; July 10th, 2017 at 02:32 PM.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    If you want to use the moby word list from post #2, extract the .fic file from the downloaded zip file. This is actually a text file with one word per line. For an example of use, consider
    Code:
    #include <string>
    #include <set>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    const string dictnam = R"(c:\downloads\113809of.fic)"s;
    
    int main()
    {
    	ifstream stdict(dictnam);
    
    	if (!stdict.is_open()) {
    		cout << "Cannot open file " << dictnam << endl;
    		return 1;
    	}
    
    	set<string> dict;
    	string line;
    
    	while (getline(stdict, line))
    		dict.insert(line);
    
    	while ((cout << "Enter a word: ") && getline(cin, line))
    		cout << (!dict.count(line) ? "Not " : "") << "found" << endl;
    }
    On a windows system, use CTRL-Z to terminate the loop.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  10. #10
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    Hello there I did update my code and I will post it

    Code:
     	ifstream  dictionary ( "Napster.txt" );
    
    	dictionary.open ( "Napster.txt" );
    	
    	string words[511131];
    	string read;
    	
    	if ( !dictionary ) {
    		
    		cout << "\nCannot open file." << dictionary << endl;
    		return 1;
    	}
    what I need to do is read the txt file line by line ( because each word in on a line ) then I should start my task which is search for words in 2d arrays which were randomly filled with character . So I think the part you wrote about cout << " enter a word " will not be part of my program . tell me if I did something wrong . please notice 511131 is total number of words in my txt file .

    I couldnt test it because I had this warning which points the srand ( time (0));


    Warning C4244 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data Project

    what does this means . thx a lot for your help .
    Last edited by david16; July 11th, 2017 at 10:31 AM.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    I couldnt test it because I had this warning which points the srand ( time (0));
    You missed the cast from my previous example.
    Code:
    srand ((unsigned int) time (0));
    Why are you trying to read them into a c-style array of string? Why not use set as per my example?? I'm not sure that you can have an array that size You may need to use dynamic memory (vector or preferably set).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  12. #12
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    Actually I'm not familiar with set and vectors so I was trying to avoid using something I don't know about but is it true or not ??

    Actually I did a new project using visual studio 2017 because I noticed I had an problem with 2010 and I did pass that piece of code to a function I didnt use set or vectors ?? but when I pass it to main I get stack overflow what's wrong with it ??

    Code:
     void dict() {
    
    	ifstream  dictionary("Napster.txt");
    
    	dictionary.open("Napster.txt");
    
    	string words[511131];
    	string read;
    
    	if (!dictionary) {
    
    		cout << "\nCannot open file.\n " << endl;
    	}
    }
    Last edited by david16; July 11th, 2017 at 11:35 AM.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    I get stack overflow what's wrong with it
    The size of the array words. It's that big it's overflowed the allocated stack! That's why you'll need to use some sort of dynamic structure - and in this case a set is the best.

    Also, you don't need .open() as you have already opened the file when you define dictionary.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  14. #14
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    what is the srt used for ?? just so I can understand what I;m using

  15. #15
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    I'm getting an error is that cause I plugged it in a function ??

    Code:
     void dict() {
    
    	ifstream  dictionary ("Napster.txt");
    	
    	if (!dictionary) {
    
    		cout << "\nCannot open file.\n " << endl;
    	}
    	
    	set <string> dictionary;
    	string read;
    
    }
    
    theser are my errors 
    
    WarninAccess to the path 'c:\users\napster\documents\visual studio 2017\projects\project\debug\project.exe' is denied.

    Error C2371 'dictionary': redefinition; different basic types Project

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)