空函数错误(未使用计算的值)

时间:2014-03-06 06:17:29

标签: c

大家好我已经尝试了几个代码的变体,我无法弄清楚如何解决这些警告。

  • 目标是在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++;
}

很抱歉,长篇文章只是试图给出所有信息需要得到一个好的答案。如果需要其他信息,请给我留言。感谢。

1 个答案:

答案 0 :(得分:2)

您想要解决的大多数警告,他们都非常善于发现不良行为。在您的特定情况下:

该行

*stayCount++;

不符合你的想法。 Operator precedence说在这种情况下,++首先出现。因此,它实际上意味着:

*(stayCount++);

因此警告stayCount已更改但未使用。在任何可能不明确的表达式周围加上括号,这是其中一个时间

(*stayCount)++;

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