0

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.

2
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by tat2dlady
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;
     }

Edited by pyTony: f

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.