2015-04-22 66 views
-2

我有一個小項目要做,我創建了一個程序,當用戶鍵入一個數字時,它會說,如果它是偶數或奇數。我甚至使用過一個函數。這是我的問題:使用函數整數參數與我的C++程序

如何在程序的函數中使用整數參數? (我的程序確實工作,它只是不使用整數參數。)

說明書

收件接受一個整數參數,確定所傳遞的整數是偶數還是奇數C++函數,並顯示該確定的結果。 (提示:使用%運算符。)

確保從main()調用函數。通過向其傳遞各種數據來測試該功能。

我的代碼

#include <iostream> 

using namespace std; 

void oddEven() //My Function 
{ 
    int num; 

    cout << "Please enter a number " << endl; 
    cin >> num; 

    if (num % 2) 
    { 
     cout << "It's odd" << endl; 
    } 
    else 
    { 
     cout << "It's even" << endl; 
    }  
} 

int main() //Main program  
{  
    oddEven(); //Calling my Function 
    return 0; 
} 
+5

你甚至嘗試尋找信息[功能](HTTP:/ /www.cplusplus.com/doc/tutorial/functions/)? – NathanOliver

+0

@NathanOliver是的,是的,我做了 - 因此我爲什麼在這裏。 – Geos59

+0

@ Geos59 _「是的,是的,我做了 - 因此我爲什麼來這裏。」_顯然沒有足夠的研究,並且沒有明顯的來源:[聲明函數](http://en.cppreference.com/w/cpp /語言/功能)。 –

回答

3

樣例程序:

#include <iostream> 
using namespace std; 
void oddEven(int num/*num is integer argument*/){ 
    if (num % 2) 
    { 
    cout<<"It's odd"<<endl; 
    } 
    else 
    { 
     cout<<"It's even"<<endl; 
    } 
} 
int main() //Main program 
{ int x; 
    cout<<"Please enter a number "<<endl; 
    cin>>x; 
    oddEven(x); //Calling my Function 
    return 0; 
} 
+0

謝謝,我仍然需要更多練習與爭論。 – Geos59

0

下面的代碼

#include <iostream> 

using namespace std; 

void oddEven(int num) //My Function 
{ 
    if (num % 2) 
    { 
     cout << "It's odd" << endl; 
    } 
    else 
    { 
     cout << "It's even" << endl; 
    }  
} 

int main() //Main program  
{   
    int num; 
    cout << "Please enter a number " << endl; 
    cin >> num; 
    oddEven(num); //Calling my Function 
    return 0; 
} 
+0

你其實是抄襲其他答案,還是僅僅是一個事件? –

+0

@πάνταῥεῖ,這顯然是我自己的代碼版本。當另一個答案被提出時,我正在編輯OP的代碼,所以我甚至沒有看到它。 –

+0

是的,我知道。有時你只是遲到,但通常我會通知有關問題上出現的其他答案,即使在我正在編輯時也是如此。這可能會有重大延遲(特別是在移動設備上)。 –