大家好我已经尝试了我的代码的几个变体,我无法弄清楚如何解决这些警告。无效函数错误(不使用计算的值)
- 的目标是在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函数的结束”,但正如其他人所说的,您实际上必须显示代码这些警告让我们更具体。 –