-1

I have the same code written between the IDEs, but for some reason, they have different results.

My program checks whether the given number input is a palindrome or not, say if the user input is 101 then in Dev C++ it prints out "palindrome" but in the case of using VSCode it prints out "not a palindrome". I have checked my code a lot of times and I don't see any syntax errors. I've searched for similar cases of this problem but I can't really apply or fully understand the solutions given in the post as they're using a different programming language and I'm still new to C.

int displayRev(int nNum)
{
    int remainder, revNumber = 0;
    
    while(nNum > 0)
    {
            remainder = nNum % 10;
        nNum = nNum / 10;
        revNumber = (revNumber * 10)+ remainder;
    }
    return revNumber;
}

int display7(int nNum)
{
    int nNum1 = displayRev(nNum);
    
    if (nNum == nNum1)
    {
        printf("palindrome");
    }
    else printf("not a palindrome");
}

int main()
{
    int nNum;
    
    printf ("Enter Number: "); 
    scanf ("%d", &nNum);
   
    display7(nNum);
    
    return 0;
}

I did check for the compiler and both IDEs are using the same compiler, the version of my GCC MinGW compiler is 6.3.0. I have tried executing the code in cmd and the output is still different from that of the Dev C++. Did I make a mistake in writing the code? or there is a different problem?

  • In the `displayRev` function, what is the *initial* value of `revNumber`? Remember that uninitialized local variables really *are* uninitialized, with an *indeterminate* value (think of its value as being garbage). – Some programmer dude Nov 07 '22 at 07:22
  • The problem with garbage (indeterminate) values should have been clear if you learned how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. Being able to debug ones program is a necessary skill for any programmer. For example using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) you can step through your code, step into function calls, and monitor variables and their values to see how they change. – Some programmer dude Nov 07 '22 at 07:26
  • Hello, @Someprogrammerdude. Thank you for your feedback. I've changed the problem with the uninitialized variable, but I'm still encountering the same problem of having different outputs for the two IDEs. – KenKeneshel Nov 07 '22 at 08:05
  • What if you print out the input given, does it match with what you wrote? – Randommm Nov 07 '22 at 08:09

1 Answers1

0

You are not providing enough detail about how you invoke the programs; if you run them straight from some IDE there's a chance the input is failing.

In any case, I think you should:

  1. Check that the scanf() call actually succeeds before relying on the result,
  2. Print the actual number that was processed too, to make it easier to verify.

So change the main() to:

int main(void)
{
    int nNum;

    printf("Enter Number: "); 
    if (scanf ("%d", &nNum) == 1)
    {
       display7(nNum);
    }
    return 0;
}

and then inside the checking function make it something like:

int display7(int nNum)
{
    const int nRevNum = displayRev(nNum);
    
    printf("%d: ", nNum);
    if (nNum == nRevNum)
        printf("palindrome\n");
    else
        printf("not a palindrome\n");
}
unwind
  • 391,730
  • 64
  • 469
  • 606