Like Tree2Likes
  • 2 Post By iMalc

Segmentation Fault in Hashing problem need help

This is a discussion on Segmentation Fault in Hashing problem need help within the C++ Programming forums, part of the General Programming Boards category; The point of this code is to get 15 names and then hash them. After one name is entered I ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    Segmentation Fault in Hashing problem need help

    The point of this code is to get 15 names and then hash them. After one name is entered I get a segmentation Fault and the program crashes. any help would be appreciated.



    Code:
    //driver file
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    
    #include "hash.h"
    #include "hash.cpp"
    
    
    int main(void)
    {
    int a = 0;
    hash x;
    string name, word2;
    cout << "Enter 15 names: " << endl;
    while(a <= 14)
    {
    cin >> name;
    x.insert(name);
    a++;
    }
    x.display();
    cout << "Enter a word to search for. Enter -999 to quit." << endl;
    cin >> word2;
    x.search(word2);
    return 0;
    }
    
    
    //header file
    #include <iostream>
    using namespace std;
    
    
    
    
    #ifndef HASH
    #define HASH
    
    
    typedef string hashelement;
    
    
    class hash
    {
    public:
    
    
    hash();
    void insert(string word);
    void display();
    void search(hashelement & word);
    
    
    private:
    
    
    vector<hashelement> vec1;
    int top;
    string word;
    };
    #endif 
    
    //implementation file
    
    #include <iostream>
    #include <vector>
    #include <string>
    #include <cassert>
    using namespace std;
    
    
    #include "hash.h"
    
    
    hash::hash()
    {
    vector<hashelement> vec1;
    vec1.assign(23, "?"); 
    }
    
    
    void hash::insert(string word)
    {
    int stop = 0;
    int length = word.length()-1;
    int loc = (word[0] + word[length]) % 23;
    cout << vec1.size() << endl;
    while (stop != 1)
    {
    if(vec1.at(loc) == "?")
    {
    vec1[loc] = word;
    stop = 1;
    }
    else
    loc++;
    }
    }
    
    
    void hash::display()
    {
    for( int i = 0; i <= 22; i++)
    cout << "Postition " << i << "= " << vec1[i] << endl;
    }
    
    
    void hash::search(hashelement & word)
    {
    int length = word.length()-1;
    int loc = (word[0] + word[length]) % 23;
    int found = 0;
    while( found != 1)
    {
    if( vec1[loc] == word)
    {
    cout << word << " Word found at location: " << loc << endl;
    found = 1;
    }
    else if(vec1[loc] == "?")
    {
    cout << word << " not found" << endl;
    found = 1;
    }
    else
    loc++;
    }
    }
    Last edited by billcozbee; 05-03-2013 at 08:26 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Why does your code have no indentation? That makes it difficult to read.
    As it would be easier for me to ust move on to help someone who's code does have indentation, that is what I shall do for now.
    Salem and Elysia like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,282
    What compiler / system are you running on? Do you not have access to a source level debugger? If you are running with windows, you can download and use microsoft visual c++ express for free, and it has a very good source level debugger.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Perhaps some warnings will illuminate....

    $ g++ -Wall -Wextra -Wshadow foo.cpp
    foo.cpp: In constructor `hash::hash()':
    foo.cpp:47: warning: declaration of 'vec1' shadows a member of 'this'
    foo.cpp: In member function `void hash::insert(std::string)':
    foo.cpp:52: warning: declaration of 'word' shadows a member of 'this'
    foo.cpp: In member function `void hash::search(hashelement&)':
    foo.cpp:78: warning: declaration of 'word' shadows a member of 'this'


    > #include "hash.cpp"
    Add source files to the project, don't #include them.

    This is what indented code should look like. I moved main to the end so it would compile without your complicated build step.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <cassert>
    using namespace std;
    
    //driver file
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    //#include "hash.h"
    //#include "hash.cpp"
    
    //header file
    #include <iostream>
    using namespace std;
    
    #ifndef HASH
    #define HASH
    
    typedef string hashelement;
    
    class hash {
      public:
        hash();
        void insert(string word);
        void display();
        void search(hashelement & word);
    
      private:
        vector < hashelement > vec1;
        int top;
        string word;
    };
    
    #endif  
    
    //implementation file
    
    //#include "hash.h"
    
    
    hash::hash()
    {
        vector < hashelement > vec1;
        vec1.assign(23, "?");
    }
    
    void hash::insert(string word)
    {
        int stop = 0;
        int length = word.length() - 1;
        int loc = (word[0] + word[length]) % 23;
        cout << vec1.size() << endl;
    
        while (stop != 1)
        {
            if (vec1.at(loc) == "?")
            {
                vec1[loc] = word;
                stop = 1;
            }
            else
                loc++;
        }
    }
    
    void hash::display()
    {
        for (int i = 0; i <= 22; i++)
            cout << "Postition " << i << "= " << vec1[i] << endl;
    }
    
    
    void hash::search(hashelement & word)
    {
        int length = word.length() - 1;
        int loc = (word[0] + word[length]) % 23;
        int found = 0;
    
        while (found != 1)
        {
            if (vec1[loc] == word)
            {
                cout << word << " Word found at location: " << loc << endl;
                found = 1;
            }
            else if (vec1[loc] == "?")
            {
                cout << word << " not found" << endl;
                found = 1;
            }
            else
                loc++;
    
        }
    }
    
    int main(void)
    {
        int a = 0;
        hash x;
    
        string name, word2;
        cout << "Enter 15 names: " << endl;
    
        while (a <= 14)
        {
            cin >> name;
            x.insert(name);
            a++;
        }
    
        x.display();
        cout << "Enter a word to search for. Enter -999 to quit." << endl;
        cin >> word2;
        x.search(word2);
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    I put the tags in im not sure why there is no indentation

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    I was using dev c++ but I was not getting anything useful from the debugger

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Dev-C++ is not maintained anymore. I suggest you upgrade: SourceForge.net: IDE - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Fixing the things that Salem found when compiling this should resolve most of the problems.
    Also, an insert or hash on an empty string could be problematic.

    Why the awful hash? Any words which have the same first and last letters have an equal hash - that's very poor. Perhaps do a little research into what others have used for hashes.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with segmentation fault
    By Bacondeluxe in forum C Programming
    Replies: 6
    Last Post: 09-30-2012, 09:15 PM
  2. Segmentation fault problem
    By rhayne in forum C Programming
    Replies: 2
    Last Post: 04-07-2012, 11:58 PM
  3. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  4. problem with segmentation fault
    By cBegginer in forum C Programming
    Replies: 13
    Last Post: 05-15-2005, 11:51 AM
  5. Segmentation fault problem
    By robsmith in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 05:33 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21