程序不会执行函数,但也不会出错

时间:2014-02-16 02:54:44

标签: c++ function

我编译的代码没有错误,但是当通过执行main函数时,它不会执行函数printt()。我使用(cout >> "Test" >> endl;)来测试它会停在哪里,我可以看到在printt()内或甚至在它之后发生的任何事情都不会在正在运行的程序中打印出来。

#include <iostream>
#include <stdio.h>

using namespace std;

class Piece {

public:
int ypos, xpos, index;
string color, kind;

int getY(void)
{
    return ypos;
}

int getX(void)
{
    return xpos;
}

int adjustY(int change)
{
    ypos = ypos + change;
}

int adjustX(int change)
{
    xpos = xpos + change;
}

};


Piece p[24];

void form(void)
{
for (int i = 0; i <= 23; ++i)
{
    if (i < 4)
    {
        p[i].color = "white";
        p[i].ypos = 7;
        p[i].xpos = i * 2;
        p[i].index = i;
        p[i].kind = "peon";

    }

    else if (i < 8)
    {
        int l;
        l = i - 4;
        p[i].color = "white";
        p[i].ypos = 6;
        p[i].xpos = 1 + (l * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else if (i < 12)
    {
        int m;
        m = i - 8;
        p[i].color = "white";
        p[i].ypos = 5;
        p[i].xpos = (m * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else if (i < 16)
    {
        int n;
        n = i - 12;
        p[i].color = "black";
        p[i].ypos = 0;
        p[i].xpos = 1 + (n * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else if (i < 20)
    {
        int pp;
        pp = i - 16;
        p[i].color = "black";
        p[i].ypos = 1;
        p[i].xpos = (pp * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else
    {
        int q;
        q = i - 20;
        p[i].color = "black";
        p[i].ypos = 2;
        p[i].xpos = 1 + (q * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }
}
}


char matrix[8][8];


void printt(void)
{
for (int i = 0; i = 7; ++i)
{
    for (int j = 0; j = 7; ++j)
    {
        matrix[i][j] = '_';
    }
}
for (int c = 0; c <= 23;++c)
{
    int a, b;
    a = p[c].ypos;
    b = p[c].xpos;
    int dec1, dec2;
    if (p[c].kind == "peon")
    {
        dec1 = 1;
    }
    else
    {
        dec1 = 2;
    }
    if (p[c].color == "white")
    {
        dec2 = 1;
    }
    else
    {
        dec2 = 2;
    }
    switch(dec1)
    {
        case 1:
            switch(dec2)
            {
                case 1:
                    matrix[a][b] = 'o';
                    break;

                case 2:
                    matrix[a][b] = 'x';
                    break;

            }
            break;

        case 2:
            switch(dec2)
            {
                case 1:
                    matrix[a][b] = 'O';
                    break;

                case 2:
                    matrix[a][b] = 'X';
                    break;
            }
            break;
    }

}
cout << "   0|1|2|3|4|5|6|7|  X Position (column)(j)" << endl;
cout << endl;
cout << "0  " << matrix[0][0] << "|" << matrix[0][1] << "|" << matrix[0][2] << "|" << matrix[0][3] << "|" << matrix[0][4] << "|" << matrix[0][5] << "|" << matrix[0][6] << "|" << matrix[0][7] << endl;
cout << "0  " << matrix[1][0] << "|" << matrix[1][1] << "|" << matrix[1][2] << "|" << matrix[1][3] << "|" << matrix[1][4] << "|" << matrix[1][5] << "|" << matrix[1][6] << "|" << matrix[1][7] << endl;
cout << "0  " << matrix[2][0] << "|" << matrix[2][1] << "|" << matrix[2][2] << "|" << matrix[2][3] << "|" << matrix[2][4] << "|" << matrix[2][5] << "|" << matrix[2][6] << "|" << matrix[2][7] << endl;
cout << "0  " << matrix[3][0] << "|" << matrix[3][1] << "|" << matrix[3][2] << "|" << matrix[3][3] << "|" << matrix[3][4] << "|" << matrix[3][5] << "|" << matrix[3][6] << "|" << matrix[3][7] << endl;
cout << "0  " << matrix[4][0] << "|" << matrix[4][1] << "|" << matrix[4][2] << "|" << matrix[4][3] << "|" << matrix[4][4] << "|" << matrix[4][5] << "|" << matrix[4][6] << "|" << matrix[4][7] << endl;
cout << "0  " << matrix[5][0] << "|" << matrix[5][1] << "|" << matrix[5][2] << "|" << matrix[5][3] << "|" << matrix[5][4] << "|" << matrix[5][5] << "|" << matrix[5][6] << "|" << matrix[5][7] << endl;
cout << "0  " << matrix[6][0] << "|" << matrix[6][1] << "|" << matrix[6][2] << "|" << matrix[6][3] << "|" << matrix[6][4] << "|" << matrix[6][5] << "|" << matrix[6][6] << "|" << matrix[6][7] << endl;
cout << "0  " << matrix[7][0] << "|" << matrix[7][1] << "|" << matrix[7][2] << "|" << matrix[7][3] << "|" << matrix[7][4] << "|" << matrix[7][5] << "|" << matrix[7][6] << "|" << matrix[7][7] << "   Y Position (line)(i)" << endl;
cout << endl;
}


int main()
{
cout << "test1" << endl;
form();
cout << "test2" << endl;
printt();
cout << "End of Programm" << endl;
system ("pause");
return 0;
}

请帮助我,对不起,如果我做了一些愚蠢的事,那就是编程新手。

注意:( "End of Programm"也不会在正在运行的程序中打印)

2 个答案:

答案 0 :(得分:3)

变化

for (int i = 0; i = 7; ++i)
{
    for (int j = 0; j = 7; ++j)
    {
        matrix[i][j] = '_';
    }
}

for (int i = 0; i < 7; ++i)
{
    for (int j = 0; j < 7; ++j)
    {
        matrix[i][j] = '_';
    }
}

你创造了无限循环

答案 1 :(得分:0)

我建议打开编译器的所有警告。如果使用g ++,那么好的标志就是“-Wall”。其他编译器应该有类似的选项。

使用此标志进行编译会产生几个警告(包括那些指向MichałBanasiak指出的循环问题的警告)。修复它们将有助于使您的程序正常工作。