2014-03-06 56 views
0

大家好我已經嘗試了我的代碼的幾個變體,我無法弄清楚如何解決這些警告。無效函數錯誤(不使用計算的值)

  • 的目標是在C創建蒙提霍爾問題的模表示

問題

  • outputFinalResult功能無法給我正確的輸出

個錯誤消息

- gcc -Wall monteball.c 

monteball.c: In function ‘determineOutcomeStay’: 

monteball.c:125:7: warning: value computed is not used [-Wunused-value] 

monteball.c:127:3: warning: value computed is not used [-Wunused-value] 

monteball.c: In function ‘determineOutcomeSwitch’: 

monteball.c:134:7: warning: value computed is not used [-Wunused-value] 

monteball.c:136:3: warning: value computed is not used [-Wunused-value] 

monteball.c: In function ‘getFinalChoice’: 

monteball.c:119:1: warning: control reaches end of non-void function [-Wreturn-type] 

順便說一句,我應該擔心這個警告呢?

代碼

void outputFinalResult (int winStay, int winSwitch, int stayCount, int switchCount); 
//function is 4 print statements using arguments calculated by determineOutcome functions 
int main (int argc, char * argv []) 
{ 
    srand(time(NULL)); 
    int counter = 10000; 
    int i = 0; 
    int winStay; 
    int winSwitch = 0; 
    int stayCount; 
    int switchCount = 0; 
    int firstChoice; 
    int grandPrize; 
    int revealedDoor; 
    int finalChoice; 

    for (i = 0; i < counter; i++) 
    { 
    firstChoice = getUserChoice(); 

    grandPrize = determinePrizeLocation(); 

    revealedDoor = revealDoor (firstChoice, grandPrize); 

    finalChoice = getFinalChoice (firstChoice, grandPrize, revealedDoor); 

    if(finalChoice == firstChoice) 
    { 
     determineOutcomeStay(finalChoice, grandPrize, &winStay, &stayCount); 
    } else 
    { 
     determineOutcomeSwitch(finalChoice, grandPrize, &winSwitch, &switchCount); 
    } 

    } 

    outputFinalResult (winStay, winSwitch, stayCount, switchCount); 
    return 0; 
} 
int getFinalChoice (int firstChoice, int grandPrize, int revealedDoor) 
{ 
    int finalChoice; 
    int switchProbability = rand() % 2 + 1; // 50% chance of keeping or switching choice                        

    if (switchProbability == 1) // Represents user keeping their choice                            
    { 
    return firstChoice; 
    } 

    else if (switchProbability == 2) // Represents user switching their choice                          
    { 
    finalChoice = rand() % 3 + 1; // Randomizes finalChoice btw 1-3                            

    while (finalChoice == revealedDoor || finalChoice == firstChoice) // Ensures that finalChoice isn't the door that was eliminated or           
    {                 // the door that was initially selected                 
     finalChoice = rand() % 3 + 1; 
    } 

    return finalChoice; 
    } 
} 

void determineOutcomeStay(int choice, int grandPrize, int * winStay, int * stayCount) 
{ 
    if(choice == grandPrize) 
    { 
    *winStay++; 
    } 
*stayCount++; 
} 

void determineOutcomeSwitch(int choice, int grandPrize, int * winSwitch, int * switchCount) 
{ 
    if(choice == grandPrize) 
    { 
    *winSwitch++; 
    } 
    *switchCount++; 
} 

對不起,長的帖子只是想給所有的信息需要得到一個很好的答案。如果需要其他信息,請通知我。謝謝。

+0

編譯器抱怨函數'determineOutcomeStay','determineOutcomeSwitch'和'getFinalChoice'。他們看起來怎麼樣? – DCoder

+0

你的錯誤不在'main'中。警告提到monteball.c行125到136(這就是它後面的數字) – Cramer

+0

您應該關注「控制達到非void函數的結束」,但正如其他人所說的,您實際上必須顯示代碼這些警告讓我們更具體。 –

回答

2

大多數警告你想解決,他們很擅長髮現不良行爲。你的具體情況:

*stayCount++; 

不會做你的想法。 Operator precedence表示在這種情況下++首先出現。人機工程學實際上就意味着這一點:

*(stayCount++); 

這樣的警告,stayCount改變,但沒有使用。把括號周圍可能是不明確的任何表情,這是那個時代

(*stayCount)++; 

getFinalChoice錯誤是自explanitory之一,「控制到達非void函數的末尾」。換句話說,有一條通過函數的路徑,不會導致return被調用。我會讓你找到它;)

+0

非常感謝您在過去的一週裏多次重寫這段代碼,我一直在撕掉我的頭髮。至於getFinalChoice函數,我無法看到未檢測到的路徑是否有任何提示? – user3386754

+0

@ user3386754 1:從最後一個大括號開始,通過函數向後查找,以確定如何到達那裏。 2:繪製所有可能路徑的樹,確保每個if語句都有一個fork。 – Cramer

+0

好的,謝謝,這給了我一個很好的起點 – user3386754