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;
}

Recommended Answers

All 4 Replies

any idea to to divide the string say into blocks of 4 characters?

Use std::string::substr().

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.

Or vector<string>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.