模组功能不起作用为什么

mod function not working why?

本文关键字:为什么 不起作用 功能      更新时间:2023-10-16
#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;
}