I just found out about C ++ functions; can i use if expressions for the return values โ€‹โ€‹of a function?

What confuses me is the function isNumPalindrome (). It returns a boolean value of true or false. As I suggest using this so that I can display if it is a palindrome or not. E.g. 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; } 
+6
c ++ function
source share
9 answers

The return value for the function can be used as 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 can be more concise:

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

What you do not want to do is use these global variables that you defined. In more complex programs that will be a recipe for disaster.

Edit: you want you to edit your isNumPalindrome function to accept the number it works with:

 bool isNumPalindrom(int num) { ... } 
+11
source share

Yes, you can do such a thing.

In fact, you could only do ...

 if (isNumPalindrome()) { ... } 
+8
source share
 if(isNumPalindrome()) { cout << "Your number is a palindrome"; } else { cout << "Your number is not a palindrome"; } 
+4
source share

when a function returns a type, you may think that this function is replaced by the return value and type. therefore for you:

isNumPalindrome () โ†’ {true / false}

so you can write, for example:

 if(isPalindrome()) cout<<"it is!"<<endl; else cout<<"it is not :("<<endl; 
+2
source share

First, you should not use global variables for num and pwr ; you must pass them as arguments to the functions:

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

Secondly, there is no need to compare a boolean with true (or false ); just use the boolean value.

It's hard to say exactly which syntax you are trying to use if in your example, but one thing you cannot do is the if statement in the expression. In C ++, there are expressions and statements. Expressions matter; There are no allegations.

 // 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 triple? "

+2
source share

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

Here is an example of a triple operation to print the result.

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

This is a bit strange looking for a lot of newbies, but shows how ternary operators can be used to print conditional answers.

+2
source share

You can call isNumPalindrome () inside askNumber () and get the return value from isNumPalindrome () in the 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 only ask askNumber ()

0
source share

In one sentence:

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

0
source share

one more 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; } 
0
source share

All Articles