954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with String Comparison

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.

tat2dlady
Light Poster
32 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

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;
	 }
Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

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

tat2dlady
Light Poster
32 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You