使用c ++进行宾果游戏测试

时间:2013-12-01 01:15:42

标签: c++ testing

我在几年没有代码的情况下进行测试,进行迷你宾果游戏测试......

最后,我有:

#include <iostream>

using namespace std;

int main()
{
 int cartela[5][3] = { {1, 2, 3}, {6,7,8}, {11,12,13}, {14,15,16}, {17,18,19} } ;
 int sorteado[8];
 int detectado[5];
 cout << "Insira os 8 numeros sorteados: ";
 cin >> sorteado[0] >> sorteado[1] >> sorteado[2] >> sorteado[3] >> sorteado[4] >> sorteado[5] >>sorteado[6] >> sorteado[7];

for (int tsort=0; tsort<9; tsort++) {
    for (int i=0; i<6; i++) {
        for (int j=0; j<4; j++) {
        if (cartela[i][j] == sorteado[tsort])  {
                  detectado[i]++;
                                    }
    }
}
}
cout << "cartela 1: " << detectado[0]<<"\n";
cout << "cartela 2: " << detectado[1]<<"\n";
cout << "cartela 3: " << detectado[2]<<"\n";
cout << "cartela 4: " << detectado[3]<<"\n";
cout << "cartela 5: " << detectado[4]<<"\n";
return 0;
}

或者pastebin:http://pastebin.com/TLYAZTtE

如果你注意到,目标是得到用户在已装载的游戏中输入的数字的数量(cartela [5] [3])。

并且,结果仅在第2和第3场比赛中达到。 其余的,结果是令人难以置信的。 LOL

任何人都可以帮我找到我的错误?

谢谢!

1 个答案:

答案 0 :(得分:1)

你的索引错了。例如,看看这个:

int sorteado[8];

即。索引为8个值,但在for循环中:

for (int tsort=0; tsort<9; tsort++)

你正在循环9个元素,从0到8来纠正它:

for (int tsort=0; tsort< 8; tsort++) {
    for (int i=0; i< 5; i++) {
        for (int j=0; j< 3; j++) {
        if (cartela[i][j] == sorteado[tsort])  {
              detectado[i]++
        }
}

}

现在,真的,你的问题是什么?

<强>已更新

您忘记初始化“detectado”,因此,请将您的代码更新为:

int detectado[5] = {0};
                ^^^^^^
相关问题