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?

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

You could possiby try the modulus operator...I'm not positive as I am currently just starting out learning C++ but it seems like it might work especially if you know how many numbers is going to be in the number entered.

``````int numberAssign(long number)
{
int number;     // original number entered
int first;
int second;      // etc.
int residue;

cin >> number;
while(residue > 0)
{
first = number%10;
residue = number/10;
second = residue%10;
residue/=10;     // and so forth...
}
}``````

Edited by Nick Evan: Wrap your code in [code].. code here .. [/code] tags

2
Contributors
1