| | |
Help with String Comparison
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 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.
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.
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)
#include <iostream>
C++ Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- String comparison in two files (C++)
- string-comparison methods and the techniques (Java)
- search in a string (Assembly)
- string compare (C++)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- string comparison in VB.net (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: Error
- Next Thread: Still working on apptlist program using queue
Views: 4140 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





