TransWikia.com

Have to print to screen for program to work

Stack Overflow Asked on December 13, 2021

So, my program has run into some wierd issue where if I write

#include <iostream>
#include <string>

using namespace std;

bool isPalindrome(int);

int main()
{
    int biggestPalindrome = 0;
    for(int i = 999; i > 0; i--)
    {
        for(int j = i; j > 0; j--)
        {
            cout << "Hello" << endl; // This part
            if(isPalindrome(i * j) && i * j > biggestPalindrome) biggestPalindrome = i * j;
            if(i * j < biggestPalindrome) break;
        }
        if(i * (i-1) < biggestPalindrome) break;
    }
    cout << biggestPalindrome;
}

bool isPalindrome(int number)
{
    int temp, rev, digit;
    temp = number;
    do
    {
        digit = number % 10;
        rev = (rev * 10) + digit;
        number /= 10;
    } while(number != 0);
    return temp == rev;
}

The program runs fine in this case, but when I comment out the 15th line (‘cout << "Hello" << endl;’) the program directly returns 0.

One Answer

In the function isPalindrome, the value of rev is used without initializing that.

It means you are using indeterminate value.

Add ret = 0; before the loop to fix.

Answered by MikeCAT on December 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP