简体   繁体   中英

how to call my boolean function so i can use if else for the cout in c++

that my code

so in the main function I want to call the bool function but I don't know how

To call a boolean function you just type the function name with the relative parameters in the brackets

Here is an example:

bool isEven(int number){..}

can be called with

isEven(3)

You can use the following program:

#include <iostream>
using namespace std;
//forward declare the function
bool palindrome (string a);
int main() {
    string a;
    cout<<"Masukkan kata : ";
    cin>> a;
    
    if (palindrome(a) == true)//call the function and check the return value. 
    {
        cout<<"Kata tersebut termasuk palindrome ";
    }
    else
        cout<<"Kata tersebut tidak termasuk palindrome";
}
bool palindrome (string a) {
    int b;
    b= a.length();
    if (b == 0)
        return 1;
    else if (a[0] != a[b - 1])
        return 0;
    else
        return palindrome (a.substr(1, b - 2));
}

The output of the above program can be seen here .

You should define the function before the main function.

#include <iostream>

using namespace std;

bool isPalindrome(std::string &s) {
  // ...
  return false;
}

int main() {
  std::string s;
  cin >> s;
  if (isPalindrome(s)) {
    std::cout << "..." << std::endl;
  } else {
    std::cout << "..." << std::endl;
  }
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM