Member Avatar for tat2dlady

Can anyone tell me how to compare a string to an integer? I have a SSN stored as a string, SSN[10]. I need to compare each number in the SSN. What I need to do is to add up the ASCII values of the SSN to use for a hashing function. For example, the SSN is 222116666. I need to add the ASCII values up: 50 + 50 + 50 + 49 + 49 + 54 + 54 + 54 + 54 = 464.
strcmp() does not work comparing a string and an integer. I have the string class and tried it that way (declaring SSN as a string type) but then it will not let me use get(SSN) to read the SSN. Any suggestions? Thanks a lot.

Member Avatar for Tight_Coder_Ex

Fortunately all data is numeric, string or otherwise. Therefore casting a single element of SSN to int allows you to add that single byte to an integer as if it were original an integer.

#include <iostream> using namespace std; int main() { int Result, Pntr; string SSN = "112226666"; Result = Pntr = 0; while (SSN [Pntr]) Result += (int) SSN [Pntr++]; cout << Result << endl; return 0; } 
Member Avatar for tat2dlady

Thanks a lot for the input, Tight Coder. It worked great! Solved a very simple problem for me. Thanks again! :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.