Convert String to a Unique Value

Hey guys. I'm working on a class assignment and I'm having trouble converting a string - "Bread" for example, to a unique numerical value - 529384002 for example.

I would assume it's not terribly difficult, but I'm relatively new to C++ and Google hasn't been helpful.

Thanks in advance for any help!
"Hash" is the search term you want.
I just wrote a little program that will convert any string of characters to it's own unique ID number that won't change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

int main()
{
	std::string myString;
	long randomizer = 100;
	
	std::cout << "Enter a string: ";
	std::getline(std::cin, myString);

	// Converts each character of string into an integer
	// and adds them together
	for (int i = 0; i < myString.size(); i++)
	{
		randomizer += myString[i];
	}

	// Make our number a little bit more unique
	randomizer *= 3.1415;
	randomizer += myString.size() * (int)myString[0];
	randomizer *= (int)myString[myString.size() - 1];

	std::cout << "Your strings unique number is: " << randomizer << std::endl;
	return 0;
}
Last edited on
Thanks a lot to both of you!
Topic archived. No new replies allowed.