0

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;
}
5
Contributors
4
Replies
6
Views
12 Years
Discussion Span
Last Post by Grunt
1

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.

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.