如何调用我的布尔函数,以便我可以在 C++ 中使用 if else 作为 cout
how to call my boolean function so i can use if else for the cout in c++
that my code
所以在main函数中我想调用bool函数但是不知道怎么调用
要调用布尔函数,您只需在括号中输入函数名称和相关参数
这是一个例子:
bool isEven(int number){..}
可以用
调用
isEven(3)
您可以使用以下程序:
#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));
}
上面程序的输出可见here.
你应该在main函数之前定义函数。
#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;
}
that my code
所以在main函数中我想调用bool函数但是不知道怎么调用
要调用布尔函数,您只需在括号中输入函数名称和相关参数
这是一个例子:
bool isEven(int number){..}
可以用
调用isEven(3)
您可以使用以下程序:
#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));
}
上面程序的输出可见here.
你应该在main函数之前定义函数。
#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;
}