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

Check digit

I am using c++ and need to devise a method to determine the check digit of a credit card. The user enters the card no. as a string. The card no is then divided into blocks of length, x. these blocks are then added.
I have the code to convert the string into and integer, however i am stuck at how to divide the string into consecutive blocks. Logically, i think i must first divide the string into these blocks then convert those smaller strings into integers.
Here is the code to convert to integer. any idea to to divide the string say into blocks of 4 characters?
thanks in advance

int convert(string str)
{
int m;
int i= 0;
int intValue=0;
int strLength = str.length()-1;
while(i<=strLength){
 m=str.at(i);
    intValue=intValue*10+(m-'0');
 i++;}
return intValue;
}
hay_man
Newbie Poster
16 posts since Jun 2006
Reputation Points: 38
Solved Threads: 0
 
any idea to to divide the string say into blocks of 4 characters?

Use std::string::substr() .

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Use std::string::substr() ...


to break the string into 4 strings of 4 characters. Store them in a string array (or 4 variables if you don't know arrays). Then work with each of the 4 strings.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Or vector

Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You