C 无效函数错误(未使用计算值)
大家好,我已经尝试了我的代码的一些变体,但我不知道如何解决这些警告C 无效函数错误(未使用计算值),c,C,大家好,我已经尝试了我的代码的一些变体,但我不知道如何解决这些警告 目标是用c语言创建monty hall问题的模块化表示 问题 MyoutputFinalResult函数无法为我提供正确的输出 错误消息 - gcc -Wall monteball.c monteball.c: In function ‘determineOutcomeStay’: monteball.c:125:7: warning: value computed is not used [-Wunused-v
- 目标是用c语言创建monty hall问题的模块化表示
- MyoutputFinalResult函数无法为我提供正确的输出
- 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++;
}
void outputFinalResult(int winStay、int winSwitch、int stayCount、int switchCount);
//函数是4个打印语句,使用determineoutcom函数计算的参数
int main(int argc,char*argv[])
{
srand(时间(空));
int计数器=10000;
int i=0;
温斯泰国际酒店;
int-winSwitch=0;
国际储蓄账户;
int开关计数=0;
第一选择;
国际特等奖;
int revealedDoor;
国际金融选择;
对于(i=0;i
抱歉,这篇文章太长了,只是想提供获得好答案所需的所有信息。如果需要其他信息,请给我留言。谢谢。大多数您想要修复的警告,它们非常擅长发现不良行为。在您的特殊情况下: 线路
*stayCount++;
不做你想做的事。表示在本例中,++是第一位的。因此,它实际上意味着:
*(stayCount++);
因此,stayCount
警告已更改,但未使用。在任何可能模棱两可的表达式周围加上括号,这就是其中之一
(*stayCount)++;
在
getFinalChoice
中,错误是自解释性的,“控件到达非无效函数的末尾”。换句话说,函数中有一条路径不会导致返回
被调用。我会让你找到的;) 编译器正在抱怨函数determineOutcomeStay
、determineOutcomeSwitch
和getFinalChoice
。它们看起来像什么?您的错误不在main
中。警告提到monteball.c第125到136行(后面的数字就是这个意思),你应该关注“控制达到非空函数的末端”,但正如其他人所说,事实上,你必须向我们展示给出这些警告的代码,以便更具体地说明。你会收到一条警告消息,提示函数X。你发布了一个不包含函数X代码的问题。公平地说,这里的人会入侵你的计算机并获取代码,但你至少可以发布你的IP地址,让他们的生活更轻松一点。对不起我的错误。尽管说了这么多讽刺的话,我还是很感激你们都愿意帮助我。我编辑了我的原始帖子,加入了必要的功能。非常感谢你。在过去的一周里,我一直在努力重写这段代码多次。至于getFinalChoice函数,我看不到未检测到的路径。你有什么提示吗?@user3386754 1:从最后一个大括号开始,通过该函数反向工作,找出如何到达那里。2:画一棵树,列出所有可能的路径,确保每个if语句都有一个fork。Hey all认为我应该正式确认所有以前的错误和警告都已解决我在getFinalChoice中返回finalChoice的位置错误,导致“控件到达非无效函数结尾警告”。它应该在第二个if语句之外。再次感谢大家的帮助。