Help with String Comparison

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Help with String Comparison

 
0
  #1
Mar 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Help with String Comparison

 
0
  #2
Mar 8th, 2005
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.

  1. #include <iostream>
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Re: Help with String Comparison

 
0
  #3
Mar 8th, 2005
Thanks a lot for the input, Tight Coder. It worked great! Solved a very simple problem for me. Thanks again! :p
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4140 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC