944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4510
  • C++ RSS
Mar 8th, 2005
0

Help with String Comparison

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
tat2dlady is offline Offline
32 posts
since Jan 2005
Mar 8th, 2005
0

Re: Help with String Comparison

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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
C++ Syntax (Toggle Plain Text)
  1. using namespace std;
  2.  
  3. int main()
  4. {
  5. int Result, Pntr;
  6. string SSN = "112226666";
  7. Result = Pntr = 0;
  8.  
  9. while (SSN [Pntr])
  10. Result += (int) SSN [Pntr++];
  11.  
  12.  
  13. cout << Result << endl;
  14. return 0;
  15. }
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Mar 8th, 2005
0

Re: Help with String Comparison

Thanks a lot for the input, Tight Coder. It worked great! Solved a very simple problem for me. Thanks again! :p
Reputation Points: 10
Solved Threads: 0
Light Poster
tat2dlady is offline Offline
32 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error
Next Thread in C++ Forum Timeline: Still working on apptlist program using queue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC