模组功能不起作用为什么
mod function not working why?
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b){
if(a%b==0){
c=1;
cout<<"Solution Available :t"<<c;
} else
{
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : n";
cin>>e>>d;
cout<<endl;
}
找到两个数字的模组而不编译程序时出错:
查找两个数字的模组而不编译程序时出错
它为我编译
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :t"<<c;
} else {
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : n";
cin>>e>>d;
fun_div(e,d);
cout<<endl;
}
您应该在询问编译错误时放置错误消息。 但是,我完全复制了您的代码并进行了编译。
另一件事是你不调用你的函数,所以我也添加了它。
作为旁注,你可以做
int fun_div(int a, int b)
{
return (a%b == 0);
}
因为如果 (a%b == 0)
是 B 的倍数,否则a
计算结果为 1。
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :t"<<c;
}
else
{ c=0; }
return c;
}
int main()
{
int e,d;
cout<<"enter two values : n";
cin>>e>>d;
c=fun_div(e,d);
cout<<endl;
}
试试这个。我认为这是你所期望的。请解释您的问题以获得更具体的答案。
我已经在函数fun_div中添加了一个函数调用。
您可能应该再添加一个更大的检查..更大的检查将确保有适当的余数可用
代码中的主要问题是你没有调用你定义的函数,这就是为什么你没有得到想要的结果,并且有一些更好的代码编写实践,你应该遵循这些做法以避免将来出错。
不要使用全局变量,如果您从函数返回结果,则从主函数显示在屏幕上。
下面给出了推荐代码,我已经更改了函数,因此它只会检查"a"是否可以被"b"整除,并将值返回给 main,以便它在屏幕上显示结果。
#include<iostream>
using namespace std;
int fun_div(int a, int b)
{
return (a%b == 0);
}
int main() {
int e, d;
cout << "enter two values : ";
cin >> e >> d;
if (fun_div(e, d))
{
cout << "Solution Exists.";
}
else
{
cout << "No Solution Exists.";
}
cout << endl;
return 0;
}
相关文章:
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 为什么二进制搜索在我的测试中不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么这段代码不起作用,我该如何解决?
- 为什么 zlib 放气初始化调用一次不起作用?
- 为什么 ulimit -v 在 Clang 的地址清理器下不起作用?
- 为什么在 while 循环中返回表达式不起作用
- 为什么这些完全相似的代码不起作用?
- 为什么我的数组双精度函数不起作用?
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 自动点击器在游戏中 - 为什么不起作用?
- 像在 Python 中一样C++循环中的参数解析。为什么不起作用?
- 多维动态数组,为什么不起作用?
- 函数A调用函数B,反之亦然(为什么不起作用)
- C++二进制树构建为什么不起作用