2014-03-06 26 views
0

大家好我已经尝试了我的代码的几个变体,我无法弄清楚如何解决这些警告。无效函数错误(不使用计算的值)

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

对不起,长的帖子只是想给所有的信息需要得到一个很好的答案。如果需要其他信息,请通知我。谢谢。

+0

编译器抱怨函数'determineOutcomeStay','determineOutcomeSwitch'和'getFinalChoice'。他们看起来怎么样? – DCoder

+0

你的错误不在'main'中。警告提到monteball.c行125到136(这就是它后面的数字) – Cramer

+0

您应该关注“控制达到非void函数的结束”,但正如其他人所说的,您实际上必须显示代码这些警告让我们更具体。 –

回答

2

大多数警告你想解决,他们很擅长发现不良行为。你的具体情况:

线

*stayCount++; 

不会做你的想法。 Operator precedence表示在这种情况下++首先出现。人机工程学实际上就意味着这一点:

*(stayCount++); 

这样的警告,stayCount改变,但没有使用。把括号周围可能是不明确的任何表情,这是那个时代

(*stayCount)++; 

getFinalChoice错误是自explanitory之一,“控制到达非void函数的末尾”。换句话说,有一条通过函数的路径,不会导致return被调用。我会让你找到它;)

+0

非常感谢您在过去的一周里多次重写这段代码,我一直在撕掉我的头发。至于getFinalChoice函数,我无法看到未检测到的路径是否有任何提示? – user3386754

+0

@ user3386754 1:从最后一个大括号开始,通过函数向后查找,以确定如何到达那里。 2:绘制所有可能路径的树,确保每个if语句都有一个fork。 – Cramer

+0

好的,谢谢,这给了我一个很好的起点 – user3386754