Jump to content

issue with my Hash

Mrcrysis2000

Trying to make a hash function that will take in a name and convert it to ASCII (with a random variable on the end) 

 

#include <iostream>
#include <string>
using namespace std;
 
int hasher(string name)
{
int integer;
    for (int counter = 0; counter >= name.length(); counter++)
    {
        integer = integer + ((int)name[counter] * counter);
    }
    cout << integer;
}
 
int main ()
{
    string name;
    cout << "Input your name";
    cin >> name;
    hasher(name);
}
 
 
Any idea where ive gone wrong and get the same number for any name i put in??
 
Thanks 
Link to comment
Share on other sites

Link to post
Share on other sites

Have you stepped through this code in a debugger?

 

Lol, you don't need a debugger for this.

Link to comment
Share on other sites

Link to post
Share on other sites

Since people only kind of answered this...

 

The conditional on your for loop is wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

Lol, you don't need a debugger for this.

You never need a debugger, that doesn't mean it won't help you find the problem and do it faster than just staring at the code.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

You never need a debugger, that doesn't mean it won't help you find the problem and do it faster than just staring at the code.

 

Not in this case.

Link to comment
Share on other sites

Link to post
Share on other sites

hasher function should look like this:

 

int hasher(string name){    int integer = 0;    for (int counter = 0; counter < name.length(); counter++)    {        integer = integer + ((int)name[counter] * counter);    }    cout << integer;    return integer;}

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."


- Albert Einstein

Link to comment
Share on other sites

Link to post
Share on other sites

Not in this case.

 

So debugging wasn't needed, but taking the time to ask on the forum was?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×