2016-11-03 157 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; 
} 
相關問題