【问题标题】:how to call my boolean function so i can use if else for the cout in c++ [closed]如何调用我的布尔函数,以便我可以将 if else 用于 c++ 中的 cout [关闭]
【发布时间】:2021-12-18 12:17:19
【问题描述】:

that my code

所以在主函数中我想调用 bool 函数但我不知道怎么做

【问题讨论】:

  • 请查看您的 C++ 课程资料。正确的函数调用是palindrome(a),使用前需要定义函数。
  • Please post text as text. 图像难以访问、难以搜索且难以处理。

标签: c++ string function boolean void


【解决方案1】:

要调用布尔函数,只需在括号中输入函数名称和相关参数

这是一个例子:

bool isEven(int number){..}

可以调用

是偶数(3)

【讨论】:

    【解决方案2】:

    您可以使用以下程序:

    #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

    【讨论】:

      【解决方案3】:

      你应该在主函数之前定义函数。

      #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;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-13
        • 2020-01-16
        • 1970-01-01
        • 2011-01-28
        • 2023-02-07
        • 2013-07-12
        • 1970-01-01
        • 2020-02-15
        • 1970-01-01
        相关资源
        最近更新 更多