2016-11-03 161 views
-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 c; 
    int e,d; 
    cout<<"enter two values : \n"; 
    cin>>e>>d; 
    cout<<endl; 
} 

误差在寻找两个数字的MOD,而不是编译程序:在寻找两个数字的MOD,而不是编译程序mod功能不工作为什么?

+0

你可以缩进代码吗? – Danh

+0

而你没有在'main'阴影'int c;'中调用'fun_div' – Danh

+0

'int c;'全局。这可能会导致未来的悲伤。 – user4581301

回答

0
#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的函数调用。

+0

代码只回答[Cargo Cult Programmers](https://en.wikipedia.org/wiki/Cargo_cult_programming)。推荐一个编辑来解释你改变了什么以及为什么。 – user4581301

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 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)将评估为1,如果a是B和0的倍数,否则。

0

你应该添加一个检查哪一个更大..大检查将确保有可用的正确余

0

那么在你的代码的主要问题是,你是不是要求的功能,你这就是为什么你没有得到期望的结果,并且有一些更好的编写代码的做法,你应该遵循以避免将来出现错误。

不要使用全局变量,如果你从函数返回结果,然后从主函数显示在屏幕上。

推荐的代码在下面给出,我已经改变了函数,所以它只会检查'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; 
} 
相关问题