正如title说的,我有一个非常简单的for循环,其中一个累积变量+=计算的值。但是,每次迭代之后,我的变量将被重置为零。为什么?
这是我的循环:
int score_board(board* b)
{
int i;
int total_score = 0;
for(i = 0; i < 4; i++)
{
printf("CURRENT TOTAL = %d\n",total_score);
total_score += score_line(grab_line(b, i, 0));
total_score += score_line(grab_line(b, i, 1));
printf("scored row and columnn %d\tcum total=%d\n", i, total_score);
}
return total_score;
}下面是我得到的输出,证明了虽然在一次迭代中total_score确实被设置为我想要的值,但是它在重置for循环时失去了它的值:
CURRENT TOTAL = 0
line score = 11000
line score = 40000
scored row and columnn 0 cum total=51000
CURRENT TOTAL = 0
line score = 60000
line score = 50000
scored row and columnn 1 cum total=110000
CURRENT TOTAL = 0
line score = 20000
line score = 40000
scored row and columnn 2 cum total=60000
CURRENT TOTAL = 0
line score = 50000
line score = 50000
scored row and columnn 3 cum total=100000编辑:找到了!在score_line内部,我最终返回的int没有初始化为零。菜鸟的错误。
谢谢大家。
发布于 2014-04-24 09:35:42
以“长”作为评论
暂时注释掉对score_line和grab_line的调用并替换它们--这个整数.
total_score += 42; // score_line(grab_line(b, i, 0));
total_score += 42; // score_line(grab_line(b, i, 1));看看你是否得到了相同的结果,如果你没有重新启用一个score_lines和再次测试,等等.将问题细分,直到找到违规的函数调用为止。
https://stackoverflow.com/questions/23264451
复制相似问题