-3

Code:

while (CheckPalindrome == false)
{
    cout << "hi";
    i = number;
    r = RevNum(i);
    adds++;
    CheckPalindrome(i);
}
bool CheckPalindrome(int i)
{
    bool Check = false;
    if (i == rev)
    {
        Check = true;
    }
    return Check;
}

For some reason it just skips right over the while loop. I'm not sure if it is a problem with the boolean or a problem with the syntax in the while loop.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
ChessRobot
  • 11
  • 1
  • 4
  • 1
    First of all, that's not a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and also [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jul 31 '18 at 03:08
  • 3
    As for your problem, what does the expression `CheckPalindrome` on its own do? How do you call a function? Perhaps it's time to go back to your tutorial or text-book or class-notes and re-read them? And if you're just guessing about things, then [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn *properly*. – Some programmer dude Jul 31 '18 at 03:09
  • You called the function, but then you discard the result. Your loop condition compares the function to `false` instead of its return value. – eesiraed Jul 31 '18 at 03:28

2 Answers2

1

CheckPalindrome is a function. By itself, the name of a function evaluates to the address of that function. Since that's a pointer type, evaluating it as the condition of an if/while is equivalent to comparing it to a null pointer, so your while loop is essentially the same as:

while (checkPalindrome == nullptr)

The address of checkPalindrome isn't going to change while the loop is executing, so this can only do one of two possible things: either checkPalindrome is at an address that compares equal to a null pointer1, and the loop never executes at all, or else it compares as not-equal to a null pointer, and you get an infinite loop.


1. There's probably room for argument that this should never happen/isn't really allowed, but even if it did, it wouldn't really help anything.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

In order to test the Boolean response of the Check Palindrome function, you must actually call the function.

Instead of having

while(CheckPalindrome == false)

change it to

while(CheckPalindrome(someVariable) == false)

and then you can modify the someVariable within the function.

But of course, since your function returns a Boolean, the proper syntax is to write:

while ( !CheckPalindrome(someVariable) )

Without that leading ! it'd be while (true). The ! makes it while not true aka while (false).

Azeem
  • 11,148
  • 4
  • 27
  • 40
MarcusOuelletus
  • 150
  • 2
  • 8