1

So I've tried to figure out what exactly the professor was writing on the board and how it answers the lab assignment we are to do.

This is the lab assignment:

Create a Hash Table and Hash map that holds all of the WORDS in the (given below) Declaration of Independence. Handle collisions using the chain method. (Note we will not be modifying this table nor doing deletions!) Programmatically answer the following questions:

  1. What is the size of your hash table?
  2. What is the longest collision (ie. Chain)
  3. What is the most frequently used word and how did you determine it?

Create a (second) Hash Table that holds all of the LETTERS in the Declaration of Independence.

  1. What is the size of your hash table

  2. What letter has the longest collision?

And this is the pseudo-code with some modifications that I did to fix some errors:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>

using namespace std;

class Translate
{
    string word;

public:
    int trans(string word);
    w = word.charAT(0);  //gives a letter
    return #num;
};

class HashTable
{
    int size();
    int collision();
    int length();
    char fword();

public:
    Translate t;
    list<string> hashTable[29];
    bool insert(string word)
    {
         hashTable[t.trans(word)].push_back(word);
         return true;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    HashTable h;
    open file f("hash.txt");
    //h.insert(word)
    while (!f.eof())
    {
        h.insert(f.word());
    }

    cout << h.size;
    cout << h.collision.length;
    cout << h.fword;

    return 0;
}

The errors that I have are:

Error 15 error C1903: unable to recover from previous error(s); stopping compilation Error 5 error C2014: preprocessor command must start as first nonwhite space
Error 4 error C2059: syntax error : 'return'
Error 13 error C2065: 'f' : undeclared identifier
Error 10 error C2065: 'file' : undeclared identifier Error 8 error C2065: 'open' : undeclared identifier Error 6 error C2143: syntax error : missing ';' before '}'
Error 1 error C2143: syntax error : missing ';' before '='
Error 11 error C2146: syntax error : missing ';' before identifier 'f'
Error 9 error C2146: syntax error : missing ';' before identifier 'file'
Error 14 error C2228: left of '.eof' must have class/struct/union
Error 3 error C2238: unexpected token(s) preceding ';'
Error 7 error C2238: unexpected token(s) preceding ';'
Error 12 error C3861: 'f': identifier not found
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 19 IntelliSense: '#' not expected here Error 17 IntelliSense: class "std::basic_string, std::allocator>" has no member "charAT"
Error 21 IntelliSense: expected a ';'
Error 18 IntelliSense: expected a declaration
Error 22 IntelliSense: identifier "f" is undefined
Error 20 IntelliSense: identifier "open" is undefined
Error 16 IntelliSense: this declaration has no storage class or type specifier

I've never used .c_str and I'm still pretty new to C++ so my knowledge is limited. I can tell that there are places that need an identifier but I think there is a better way to create a "open file". My previous knowledge is C#, HTML, and some Python in which C++ is giving me some difficulty in learning and understanding. Any help and/or insight would be greatly appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • there are way too many errors, I recommend you get a good introductory C++ book to get some basic knowledge of the language, see for example http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – vsoftco Mar 29 '15 at 23:33
  • 1
    Don't be overwhelmed by large numbers of errors in the compiler output. Fix the *first* error listed (because that's the one that triggers the rest of them), and then recompile. Repeat. – Greg Hewgill Mar 29 '15 at 23:36
  • `open file f("hash.txt");` This line is definitely wrong. – drescherjm Mar 30 '15 at 01:03
  • Also what are you doing in your public in class Translate. You can not just put random statements in the class definition. I assume you were trying to declare and define a class member function trans but forgot you have to use {} – drescherjm Mar 30 '15 at 01:11

2 Answers2

0

Code is too mangled to understand. However, I'm trying my best to help with the little knowledge of mine on C++ and hash.

Proposed Code Modification

  1. Program entry point : instead of int _tmain(int, _TCHAR*), use int main().This should guarantee you the ability to test things out should you migrate to non-windows compiler. Source : Unicode _tmain vs main

I would like to help with the remainder, however, the code posted is way too unintelligible. Would be kind if the algorithm is posted for reference.

Community
  • 1
  • 1
Nicky HFE
  • 143
  • 9
0

There are a few things you should change:

  1. Assuming trans() is supposed to be a function definition, not a declaration, and the lines following it are supposed to be the body:
    1. Unless you specifically want to copy the passed string, you should use const string& instead of string.
    2. It should have braces.
    3. w is a char.
    4. std::string defines operator[], so it can be indexed like an array.
    5. I'm not sure what #num is (I assume it's from Python, but I'm not familiar with that), so I'm not sure how you intend to calculate the return value.
      [I will thus assume that you want to return w, but as an int instead of a char. If this is the case, it would be simpler to just return word[0];.]
  2. There are a few issues with HashTable's members.
    1. Member functions size(), collision(), length(), and fword() are private. This doesn't appear to be intentional.
    2. Member variables t and hashTable are public, when you likely wanted them to be private. Again, this doesn't appear to be intentional.
    3. The functions aren't actually defined anywhere, unless you didn't show their definitions. This will cause a linking error when you call them.
  3. While this doesn't need to be changed, there's no reason for HashTable::insert() to actually return a value, if it's hard-coded to always return true. Also, as mentioned in 1.1 above, the parameter should probably be const string&.
  4. _tmain() and _TCHAR are a Microsoft extensions, which is available on Visual Studio and some (but not all) compilers aiming for compatibility with it (such as C++Builder). If you want your code to be platform-independent, you likely want main(). [Note that this doesn't need to be changed. If you're only compiling with Visual Studio, you can leave it as is. If you want platform independence, you can easily define _tmain and _TCHAR yourself.]
  5. Opening a file:
    1. Neither open nor file are keywords in C++, nor are they types (although FILE is a C type, it doesn't appear to be what you want). You appear to want std::ifstream.
    2. You shouldn't use !f.eof() as a condition in a while loop, because eofbit won't be set until after reading fails.
    3. fstream has no member function word(). However, the extraction operator, operator>>() will read a single word at a time, if given a parameter that can accept one.
  6. HashTable::size(), HashTable::collision(), HashTable::length(), and HashTable::fword() are functions. To call them, you use operator(). If you just use a function's name directly, you don't call it, but instead refer to it (this can be used to create a function pointer or function reference).
  7. int has no member function length(). Therefore, you cannot call h.collision().length(). In C++, if you chain function calls like that, each function in the chain is treated as if it were a member function of the directly preceding type, not the leftmost type; this means that for every function after the first, the return type of the preceding function is used. (In this case, h.collision() returns an int, so .length() attempts to call member function int::length(). int isn't a class type, and thus doesn't have any member functions.)

So, considering these, your code can be modified as follows:

// Assuming your stdafx.h contains "#include <string>" and "#include <tchar.h>".
// If it doesn't, either put them there, or #include them here.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>

// #4: Defining _tmain and _TCHAR
#ifndef _tmain
    #define _tmain main
    typedef char _TCHAR;
#endif


using namespace std;

class Translate
{
    string word;

public:
    // #1: Fixing trans().
    int trans(const string& word)
    {
        char w = word[0];  // First letter of word.
        return w;     // Will be promoted to int.
    }
};

class HashTable
{
// #2: Making member functions public, and member variables private.
    Translate t;
    list<string> hashTable[29];

public:
    int size();
    int collision();
    int length();
    char fword();

    // #3: Making word a const reference.  Changing return type to void.
    void insert(const string& word)
    {
         hashTable[t.trans(word)].push_back(word);
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    HashTable h;

    // #5.1: Opening the file.
    ifstream f("hash.txt");

    //h.insert(word)

    // #5.2 & 5.3: Reading a word.
    std::string word;
    while (f >> word)
    {
        h.insert(word);
    }

    // #6: Calling functions.
    cout << h.size();
    cout << h.collision(); // #7: Assuming you wanted to output both h.collision() and
    cout << h.length();    //     h.length(), I put them on separate lines.
                           //     If you actually DID want h.collision().length(), then
                           //     h.collision() should return a type (or reference to a type)
                           //     with member function length(), or be an instance
                           //     (or reference to an instance) of a class with member function
                           //     length() (instead of being a function).
    cout << h.fword();

    return 0;
}

You still need to provide bodies for HashTable's member functions, apart from insert(), as well as make any other modifications you desire. You might also want to remove member word from Translate, if it doesn't actually need to store a string.

Community
  • 1
  • 1