c++ - 如何检查函数参数中的变量是否与 C++ 中函数外部的变量匹配?
问题描述
例如,如果在我的 main() 函数的开头,我有一个用户 cin 一个 int x 的值,然后我有一个类似 same(int y) 的函数,它使用从文件中读取的变量 y,如何我可以检查(在 same() 函数内)参数 int y 是否与 int x 相同?
解决方案
你可以试试:
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;
}
我认为这应该有效,但如果它没有,至少我希望它可以帮助你。
推荐阅读
- or-tools - 如何使用自定义决策构建器配置路由问题?
- python - 如何在 kv 文件中创建变量/属性?
- android - 在模块 (dev.dworks.libs:volleyplus:0.1.4) 和 volley-1.1.0 中发现重复的类 com.android.volley
- stanford-nlp - 尝试进行 POSTtagging 时出错:加载标记器模型时出错(可能缺少模型文件)
- node.js - AWS Lambda nodejs - 来自等待异步函数的返回值问题
- python - How can I pass a client-side parameter to a server-side route without using forms?
- windows - Is there any way to detect last packet of Bulk OUT transfer in USB from Host to Device?
- macos - 外部 SSD 中 APFS 卷的自定义路径
- python - Filling between curves only when a given curve is larger than the other
- python-3.x - 如何将databricks中的ppt文件保存到我的用户文件夹中?