2014-01-29 471 views
-1

我只是無法弄清楚我在這裏做錯了什麼,但將任何參數放入我的嘗試函數中都會給我一個錯誤。爲什麼我不允許給我的函數參數?

#include <iostream> 
#include <cstdlib> 
#include <ctime> 

using namespace std; 

int guesses = 0; 

int attempt(int guess) { 
    if(guesses > 0) { 
      cout << "Take a guess!"; 
      cout << endl; 
      cin >> guess; 
      } 
} 

int main() { 

    int answer = (rand() % 10) + 1; 
    int guess; 

    srand(time(0));  

    cout << "I am thinking of a number between 1 and 10. Try to guess it in 3 attempts!"; 
    cout << endl; 
    attempt(); 

    cin.get(); 
    return 0; 
} 

我要補充一點,我是一個初學者,所以請解釋這對我來說,一個5歲。

+1

一個非常基本的5yo類型問題:你得到什麼錯誤? – 2014-01-29 02:30:33

+0

這與ELI5沒有任何關係,是嗎? – chris

+0

'int attempt(int guess)'應該是'int attempt(int&guess)',而在main,'attempt();'應該傳遞'int&'。在編寫代碼之前學習C++的基礎知識會更好。 – Mine

回答

1

當聲明與值參數的函數,如

void func(int i) 

您聲明輸入

#include <iostream> 

void func(int i) 
{ 
    // the 'i' you see here is a local variable. 
    i = 10; 
    // we changed the local thing called 'i', when 
    // we exit in a moment that 'i' will go away. 
} 

int main() 
{ 
    int i = 1; 
    func(i); 
    // if we print 'i' here, it will be our version of i. 
    std::cout << "i = " << i << '\n'; 

    return 0; 
} 

i的參數func是本地func;這會讓新的C++程序員感到困惑,特別是在上面的場景中,它看起來像是將i本身傳遞給func,而不是通過main :: i的值。當你進一步學習C/C++時,你會發現'參考'和'指針'類型,它們允許你實際地將變量轉發給函數而不是它們的值。

當你的函數實際上正在獲取一個用戶輸入值,然後將它傳遞給調用者或返回它。

int attempt() 
{ 
    std::cout << "Take a guess!\n"; 

    int guess; 
    std::cin >> guess; 

    return guess; 
} 

我們告訴大家,「嘗試」不帶參數的編譯器和將一個int值。然後在函數的最後,我們使用return來做到這一點。

return只能傳回一個值 - 稍後您將瞭解指針和引用,這將允許您返回大於單個值的內容。

要獲得這個價值,你的主:

int main() { 

    int answer = (rand() % 10) + 1; 

    srand(time(0));  

    std::cout << "I am thinking of a number between 1 and 10. Try to guess it in 3 attempts!"; 
    std::cout << endl; 

    int guess = attempt(); 
    std::cout << "You guessed: " << guess << '\n'; 

    cin.get(); 
    return 0; 
} 

或者把變量的聲明,並使用了兩行:

int guess; 
... 
guess = attempt; 

一個重要的事情,從這個全部拿走的是,你可以在不同'範圍'中使用相同名稱的不同變量:

int i = 1; // global, visible to all functions in this compilation unit. 

int main() 
{ 
    srand(time()); 
    int i = 2; 

    if ((rand() % 4) == 0) // 1 in four chance 
    { 
     // this is essentially a new scope. 
     int i = 10; 
    } 

    // here, i = 2. 
} 

void foo() 
{ 
    // here, i = 1 - we see the global since we didn't declare our own. 
} 

void foo2(int i) 
{ 
    // here, i is whatever value we were called with. 
    if (i == 1) 
    { 
     int i = 99; 
    } 
    // we're back to the i we were called with. 
} 
0
  1. 你發揮

    int attempt(int guess) 
    ^^^ 
    

    需要返回int值。

  2. 當你打電話的主體這一功能,帶輸入參數正確地調用它,所以更改

    attempt(); 
    

    attempt(guess); 
    
+1

雖然是一個好點,但這不是一個保證錯誤。這通常是一個警告。 – chris

+0

@chris更新。謝謝。 – herohuyongtao

3

你會喜歡的東西更好:

int attempt() { 
    int guess; 
    cout << "Take a guess!\n"; 
    cin >> guess; 
    return guess; 
} 

調用它:

guess = attempt(); 

有在傳遞當前guess到嘗試是沒有意義的,它只會被反射回給調用者,如果它是一個基準參數,它不是。

我也不完全某些你希望只要求已經取得時的猜測,但是這是你的舊代碼的效果估計值來獲得什麼。

其他問題,你可能有:

srand之前調用rand。這意味着你將在每次運行中得到相同的隨機數,因爲rand的行爲沒有設置種子就好像你叫srand(1)一樣。

guesses變量設置爲零並且從未更改過。大概你會在解決這個問題的時候去解決這個問題,以實現「僅僅三種猜測」的功能(假設在main有一個循環)。

0

你的函數被聲明爲

int attempt(int guess); 

即它具有:1)接受int類型的參數,並2)返回int類型的對象。

和1)被不帶參數

attempt(); 

2),並且不返回int類型的任何對象。

相關問題