gpt4 book ai didi

c - 无效函数错误(未使用计算值)

转载 作者:太空宇宙 更新时间:2023-11-04 08:44:29 24 4
gpt4 key购买 nike

大家好,我已经尝试了我的代码的几个变体,但我不知道如何解决这些警告。

  • 目标是用 c 语言创建 monty hall 问题的模块化表示

问题

  • 我的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++;
}

很抱歉这篇很长的帖子只是试图提供所有需要的信息才能得到一个好的答案。如果需要其他信息,请给我发消息。谢谢。

最佳答案

大多数您想修复的警告,它们都非常擅长发现不良行为。在您的特定情况下:

线

*stayCount++;

并没有按照你的想法去做。 Operator precedence说在这种情况下++ 先出现。因此,它实际上意味着:

*(stayCount++);

因此警告 stayCount 已更改但未使用。在任何可能有歧义的表达式周围加上括号,这就是其中之一

(*stayCount)++;

getFinalChoice 中,错误是 self 解释的,“控制到达非空函数的结尾”。换句话说,有一条通过函数的路径不会导致 return 被调用。我会让你找到它;)

关于c - 无效函数错误(未使用计算值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22216578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com