大家好我已經嘗試了我的代碼的幾個變體,我無法弄清楚如何解決這些警告。無效函數錯誤(不使用計算的值)
- 的目標是在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++;
}
對不起,長的帖子只是想給所有的信息需要得到一個很好的答案。如果需要其他信息,請通知我。謝謝。
編譯器抱怨函數'determineOutcomeStay','determineOutcomeSwitch'和'getFinalChoice'。他們看起來怎麼樣? – DCoder
你的錯誤不在'main'中。警告提到monteball.c行125到136(這就是它後面的數字) – Cramer
您應該關注「控制達到非void函數的結束」,但正如其他人所說的,您實際上必須顯示代碼這些警告讓我們更具體。 –