首页 > 解决方案 > 如何检查函数参数中的变量是否与 C++ 中函数外部的变量匹配?

问题描述

例如,如果在我的 main() 函数的开头,我有一个用户 cin 一个 int x 的值,然后我有一个类似 same(int y) 的函数,它使用从文件中读取的变量 y,如何我可以检查(在 same() 函数内)参数 int y 是否与 int x 相同?

标签: c++functionlogic

解决方案


你可以试试:

class CompareXY {
  private:
    int x = 0;
    int y = 0;

  public:
    void EnterX {
      cout << "Enter the x value: ";
      cin >> x;
    }

    void EnterY {
      //idk which way you used to get the 'y' but it goes here (and you store it in the 'int y' previously created in the private section)

    }

    void compare {
      if (x == y) {
        //whatever should happen
      }
    }
};

int main() {
  CompareXY c;
  c.EnterX();
  c.EnterY();
  c.compare();
  return 0;
}

我认为这应该有效,但如果它没有,至少我希望它可以帮助你。


推荐阅读