1. Will changing while(pass) to while(pass>0) fix that issue |
No. The simplest solution is to keep a copy of the password in another variable.
2. And shouldn't Pdig8 be where num7 is on the last line of determining the digits part of the code |
Not sure, but I think the answer is in the code I share below.
3. Instead of using that code is there a way to say if "num3 equals the reverse of num3, then its palindrome"( num3 becuase it is the number that contains 5 digits) |
That may be possible, but when you already have the number dissected into individual digits I'd keep the existing technique.
Here's part of the code which I tried:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
int copypass = pass;
while (copypass) {
copypass=copypass/10;
counter++;
}
Pdig1 = pass/10000000;
num1 = pass%10000000;
Pdig2 = num1/1000000;
num2 = num1%1000000;
Pdig3 = num2/100000;
num3 = num2%100000;
Pdig4 = num3/10000;
num4 = num3%10000;
Pdig5 = num4/1000;
num5 = num4%1000;
Pdig6 = num5/100;
num6 = num5%100;
Pdig7 = num6/10;
num7 = num6%10;
Pdig8 = num7/1;
|
Notice line 28
Pdig8 = num7/1;
of course division by 1 is not necessary, but it means the code retains a sense of symmetry, and often in programming that can be a good guide to correctness.
One thing which seemed likely, was that some of this code was done without actually testing it. What I tend to do during development is put lots of extra
printf
or
cout
statements to display values at various stages in the code, to allow me to see whether the code is working as expected. Certainly when I first tried the code originally posted above, I saw got zero in every digit and pretty soon realised that some things were not quite right.
So I do recommend extra printf statements for debugging, remove them when everything is working. An example:
1 2
|
printf("%d %d %d %d %d %d %d %d \n",
Pdig1,Pdig2,Pdig3,Pdig4,Pdig5,Pdig6,Pdig7,Pdig8);
|