tags:

views:

367

answers:

9

What I am confused on is about the isNumPalindrome() function. It returns a boolean value of either true or false. How am I suppose to use that so I can display if it's a palindrome or not. For ex. if (isNumPalindrome == true) cout << "Your number is a palindrome"; else cout << "your number is not a palindrome.";

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
 return 0;
}

#include <iostream>
#include <cmath>

using namespace std;

int askNumber();
bool isNumPalindrome();

int num, pwr;

int main()
{
 askNumber();

 return 0;
}

bool isNumPalindrome()
{
 int pwr = 0;

 if (num < 10)
  return true;
 else
 {
  while (num / static_cast<int>(pow(10.0, pwr)) >=10)
   pwr++;
  while (num >=10)
  {
   int tenTopwr = static_cast<int>(pow(10.0, pwr));

   if ((num / tenTopwr) != (num% 10))
    return false;
   else
   {
    num = num % tenTopwr;
    num = num / 10;
    pwr = pwr-2;
   }
  }

  return true;
 }
}

int askNumber()
{
 cout << "Enter an integer in order to determine if it is a palindrome: " ; 
 cin >> num;
 cout << endl;

if(isNumPalindrome(num))
{
cout << "It is a palindrome." ;
cout << endl;
}
else
{
cout << "It is not a palindrome." ;
cout << endl;
}
 return num;
}
+8  A: 

Yes, you can do such thing.

Actually you could do just...

if (isNumPalindrome()) { ... }
Paulo Santos
+2  A: 

when a function returns a type you can think of that function as being replaced by the return value and type. so for you:

isNumPalindrome() -> {true/false}

so you can write for example:

if(isPalindrome())
  cout<<"it is!"<<endl;
else
  cout<<"it is not :("<<endl;
Chris H
+9  A: 

The return value for a function can be used just like a variable of the same type.

Your main program should look something like this:

int main()
{
  int num=askNumber();
  bool isPal=isNumPalindrome(num);
  if (isPal)
  {
    //do something
  }
  else
  {
    //do something else
  }

  return 0;
}

or you could be even more succinct:

int main()
{
  if (isNumPalindrome(askNumber()))
  {
    //do something
  }
  else
  {
    //do something else
  }

  return 0;
}

What you don't want to do is use those global variables you defined. In more complicated programs that will be a recipe for disaster.

Edit: you'll want to make sure you edit your isNumPalindrome function to accept the number it's working with:

bool isNumPalindrom(int num)
{
   ...
}
miked
should i put those variables within the int main()? Would that make in a non-module/global variable and localized.
Sagistic
yes, you put the variables as local to main, and then main would pass them on to the functions that need it.
miked
+1 for showing how to assign the value of a function to a variable
Liz Albin
@miked - I am getting compilers errors saying that my variables are unreferenced/ undeclared when I put them within the main. Would I have to edit any other things besides just switching location from global->within main.
Sagistic
@Sagistic: Yes. Like miked said, you should pass the variables to the functions as arguments. For example, you would change `isNumPalindrome()` to look like the code in miked's edit, with an `int num` argument.
Chuck
+4  A: 
if(isNumPalindrome())
{
    cout << "Your number is a palindrome";
}
else
{
    cout << "Your number is not a palindrome";
}
Rachel
A: 

You could call isNumPalindrome() inside askNumber(), and have the returned value from isNumPalindrome() be used in a conditional test. It's better to pass num as an argument to isNumPalindrome though: isNumPalindrome(int num)

int askNumber()
{
 cout << "Enter an integer in order to determine if it is a palindrome: " ; 
 cin >> num;
 if(isNumPalindrome(num)){
   cout << "it is a palindrome";
 }
 cout << endl;

 return num;
}

then main can call only askNumber()

David McDavidson
+2  A: 

First, you shouldn't use globals for num and pwr; you should pass them as arguments to functions:

bool isNumPalindrome(int num);
...
num = askNumber();
isNumPalindrome(num);

Second, there's no need to compare a boolean value with true (or false); simply use the boolean value.

It's hard to tell what exact syntax you're trying to use in your example "if" statement, but one thing you can't do is have an "if" statement in an expression. In C++, there are expressions and statements. Expressions have values; statements don't.

// valid
if (isNumPalindrome(num)) {
    std::cout << '"' << num << "\" is a palindrome." << std::endl;
} else {
    std::cout  << '"' << num << "\" is not a palindrome." << std::endl;
}

// invalid
std::cout << '"' << num << (if (isNumPalindrome(num)) {
    "\" is a palindrome.";
} else {
    "\" is not a palindrome.";
}) << std::endl;

// valid, but not recommended
std::cout << '"' << num << "\" is " << (isNumPalindrome(num) ? "" : "not ") << "a palindrome." << std::endl;

For the ternary operator (?:), read "To ternary or not to ternary?"

outis
I'm getting a bunch of errors when I put the variables within main.
Sagistic
most of them are in which of, unreferenced local variable and undeclared indentifier.
Sagistic
Functions have their own scopes, they don't see any variables that are declared outside of them, unless you pass them as arguments.
David McDavidson
@Sagistic: "unreferenced local variable" is a warning meaning you declare a local variable but do not reference it. "undeclared identifier" is an error meaning you reference an identifier (such as a variable) but do not declare it. Check for typos; you should have a line such as `int num;` and refer to the variable `num` in other statements. If you need more help, first search SO and the web at large for the warning and then the error. If nothing comes up, post a new question with a minimal test case (http://catb.org/~esr/faqs/smart-questions.html#code).
outis
@Outis: Will Do. Thank you for your help. I will try to learn the exact differences of declaring variables globally/locally/block.
Sagistic
@Sagistic: the key terms to read up on are "scope", "linkage" and "storage duration".
outis
A: 

In a single sentence:

"As the condition of the if statement you can use any expression whose result, once when expression is evaluated, can be implicitly converted to 'bool'."

sinec
+1  A: 

As many people have stated, the return value of a function essentially becomes the function's value.

Here's an example of a ternary operation for printing the result.

cout << "The number " << (isNumPalindrome()) ? "is a palindrome" : "is NOT a palindrome";

This is a bit weird looking for a lot of beginners, but it shows how ternary operators can be used to print conditional responses.

Corwin
A: 

yet another solution ;-)

#include <iostream>
#include <sstream>
#include <algorithm>

bool is_palindrome( const int num )
{
    std::ostringstream os;
    os << num;
    const std::string& numStr = os.str();
    std::string reverseNumStr = numStr;
    std::reverse( reverseNumStr.begin(), reverseNumStr.end() );
    const bool result = ( numStr == reverseNumStr );
    return result;
}

int main()
{
    int num = 0;
    std::cout << "Enter an integer in order to determine if it is a palindrome: ";  
    std::cin >> num; 

    std::string inset;
    if( !is_palindrome( num ) )
    { 
        inset = "not ";
    } 
    std::cout << "It is " << inset << "a palindrome." << std::endl; 
}
Dominic.wig