15,392,636 members
See more:
I made This simple credit Card generator that generate a random 16 digit string and check LUHN Algorithm on it. I have individually tested every for loop and they perfectly follow LUHN Algorithm rules. I added a while loop on top that check that if number required to make the sum of string (except checksum) is equal to checksum(last digit as per LUHN algorithm). If that is correct then loop breaks and it print that string.
Everythings seems ok but still it doesn't give correct string.

I have manually tried without while loop and after running it few times when it gives correct that digit correctly it works.

What I have tried:

```#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int
main()
{

int sum, count = 0;
int x[100];

srand(time(0));

while (count < 1)
{

//PROCESS 1
for (int h = 0; h < 16; h++)
{
x[h] = rand() % 10;

}

//PROCESS 2
//LUNH ALGORTIHAM IMPLIMENTATION
for (int j = 14; j >= 0; j -= 2) //loop untill every second number add in itself
{
x[j] = x[j] + x[j];
}

//PROCESS 3
for (int p = 14; p >= 0; p -= 2) //Loop to add every double digit
{
if (x[p] > 9)
{
int s = x[p] % 10;
x[p] = x[p] / 10;
x[p] = x[p] + s;
}
}

for (int h = 0; h < 15; h++) //loop to add all numbers
{
sum = sum + x[h];
}

sum = sum % 10;
sum = 10 - sum;

if (sum == x[15]) //checking if last digit is the number we required to make it dividable with 10
{
count++; // if that is correct then count++ and loop break.
}
}

cout << "Credit Card number is ";

for (int k = 0; k < 16; k++) //showing numbers;
{
cout << x[k];
}
}```
Posted
Updated 23-Jan-22 4:47am
v2
Richard MacCutchan 23-Jan-22 9:18am

You need to explain what values you enter, what results you see, and why they are not correct. Please use the Improve question link above, and add complete details.
Hamza Jameel 23-Jan-22 9:33am

You mean i should explain LUHN algorithm??
I don't enter any value it generate value itself
Richard MacCutchan 23-Jan-22 9:38am

Fine, but if the value or some part of it is not correct, you need to explain exactly why. We cannot guess what happens when you run this code.
0x01AA 23-Jan-22 9:18am

Check your 'Luhn algorithm' carefully. See e.g. here:
Luhn-Algorithmus – Wikipedia[^]
Hamza Jameel 23-Jan-22 9:32am

@0x01AA my code follows LUHN Algortham step by steps. I checked it by printing that 16 digit array after every process. Without while loop (which checks that the number which we need to add in sum so it can be dividable by 10 is equal to checksum) it works perfectly. I tried running code again and again without while loop untill it generate a string whose that number is equal to checksum. then on checking on a card validator website it follows LUHN rules. Problem only occures when I try to do it with while loop so it can keep finding that number.
0x01AA 23-Jan-22 9:44am

strange. if I test your code (online e.g. https://www.onlinegdb.com/online_c++_compiler), I don't face a problem
Hamza Jameel 23-Jan-22 9:58am

No it doesn't work. it still gives the wrong check digit
0x01AA 23-Jan-22 10:05am

I don't know very much about that. But: When checking your number, I think you should never replace the card numbers digits like you do it. Read the wiki I posted above ;)
Stefan_Lang 23-Jan-22 9:58am

I'm not familiar with this algorithm, but I noticed you're printing integers without any separation in-between, and it seems these integers can be greater than 10.

Maybe you should try writing a space after each integer and find out if any of the numbers are indee greater than 10 (in cas this shouldn't happen), or if you are reading the output correctly.

## Solution 1

Look at your code:
```sum = sum % 10;
sum = 10 - sum;

if (sum == x[15]) //checking if last digit is the number we required to make it dividable with 10
{
```
What value do you get for sum == 0 or any multiple of ten? Is it a value between 0 and 9 inclusive?

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

## Solution 3

There are 3 problems in you code.
1. (Already spotted by Griff) The following sequence is inaccurate
Quote:
sum = sum % 10;
sum = 10 - sum;
2. the variable `sum` is initialized in the wrong place (should be initialized inside the `while` loop).
3. In order to compute checksum digit, you are modifying the generated number, so you are correctly validating a number but wrongly reporting it (you should instead modify a copy of the generated number.

Try
C++
```#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int
main()
{

int count = 0;
int x[100];

srand(time(0));

while (count < 1)
{
int t[16];
int sum = 0;

//PROCESS 1
for (int h = 0; h < 16; h++)
{
x[h] = rand() % 10;
t[h] = x[h];
}

//PROCESS 2
//LUNH ALGORTIHAM IMPLIMENTATION
for (int j = 14; j >= 0; j -= 2) //loop untill every second number add in itself
{
t[j] = t[j] + t[j];
}

//PROCESS 3
for (int p = 14; p >= 0; p -= 2) //Loop to add every double digit
{
if (t[p] > 9)
{
int s = t[p] % 10;
t[p] = t[p] / 10;
t[p] = t[p] + s;
}
}

for (int h = 0; h < 15; h++) //loop to add all numbers
{
sum = sum + t[h];
}

sum = sum % 10;
sum = (10 - sum) % 10;

if (sum == t[15]) //checking if last digit is the number we required to make it dividable with 10
{
count++; // if that is correct then count++ and loop break.
}
}

cout << "Credit Card number is ";

for (int k = 0; k < 16; k++) //showing numbers;
{
cout << x[k];
}
}```
v2
0x01AA 23-Jan-22 11:26am

Your answer makes sense. But I think there is no need to make a copy to calculate all that ;)
Hamza Jameel 23-Jan-22 11:55am

@0x01AA Yes we need to make copy otherwise it won't work :D
CPallini 23-Jan-22 15:44pm

Yes, you actually don't need a copy of the array. However, it is the most straightforward way to fix the OP code, in my opinion.
Hamza Jameel 23-Jan-22 11:56am

@CPallini Thankyou for helping out. I understand everything apart of logic behind declaring sum inside while loop. why can't declare it outside.
Rick York 23-Jan-22 12:20pm

You can declare it outside the loop but you must initialize it every time through the loop.
CPallini 23-Jan-22 15:46pm

Rick is right: you don't need to declare it inside the loop, provided you do initialize it inside the loop. Personally I think taht narrowing the scope of a variable is a good practice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 200 OriginalGriff 155 Richard Deeming 65 TheRealSteveJudge 50 Dave Kreskowiak 50
 OriginalGriff 1,461 Richard MacCutchan 850 Richard Deeming 295 0x01AA 295 Gerry Schmitz 290

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900