如何将'int'函数调用到'void'函数

时间:2015-10-06 20:33:06

标签: c++

我尝试使用2种不同的功能根据返回值打印某条消息。当调用另一个内部的函数时,表示事情未被声明

int isMatch(int x1, int y1, int x2, int y2)
{
    if(deck[x1][y1] == deck[x2][y2])
    {
        return 2;
    }
    else if(deck[x1][y1][0] == deck[x2][y2][0])
    {
        return 1;
    }
    else if(deck[x1][y1][1] == deck[x2][y2][1])
    {
        return 1;
    }

    return 0;

}
void printMatch()
{
    int attempt;

    isMatch(x1, y1, x2, y2);

    if(isMatch() == 2)
    {
        cout << "You have a match!";
        attempt++;
    }
}

当我这样尝试时,它仍然无法正常工作

int isMatch(int x1, int y1, int x2, int y2)
{
    if(deck[x1][y1] == deck[x2][y2])
    {
        return 2;
    }
    else if(deck[x1][y1][0] == deck[x2][y2][0])
    {
        return 1;
    }
    else if(deck[x1][y1][1] == deck[x2][y2][1])
    {
        return 1;
    }

    return 0;

}
void printMatch()
{
    int attempt;

    int isMatch(int x1, int y1, int x2, int y2);

    if(isMatch() == 2)
    {
        cout << "You have a match!";
        attempt++;
    }
}

!!!!!!!!!!更新!!!!!!!!!!!!!!!!!

int isMatch(int x1, int y1, int x2, int y2)
{

    if(deck[x1][y1] == deck[x2][y2]) /// Checks entire element (_,_)
    {
        return 2;
    }
    else if(deck[x1][y1][0] == deck[x2][y2][0]) /// Checks first element (_, )
    {
        return 1;
    }
    else if(deck[x1][y1][1] == deck[x2][y2][1]) /// Checks second element ( ,_)
    {
        return 1;
    }

    return 0;

}
void printMatch()
{
    int attempt = 0; /// increments the users attempts
    int score = 0; /// adds total score

    int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    int match = isMatch(x1, y1, x2, y2);

    if (match == 2)
    {
        attempt++;
        score = score + 2;
        cout << "\nYou have a match!"
             << "\nTotal score is: " << score
             << "\nTotal no. of attempts: " << attempt << endl << endl;
    }
    else if (match == 1)
    {
        attempt++;
        score = score + 1;
        cout << "\nYou have a match!"
             << "\nTotal score is: " << score
             << "\nTotal no. of attempts: " << attempt << endl << endl;
    }
}

2 个答案:

答案 0 :(得分:2)

将printMatch()函数更改为:

void printMatch(int x1, int y1, int x2, int y2)
{
    int attempt;

    int temp = isMatch(x1, y1, x2, y2);

    if(temp == 2)
    {
        cout << "You have a match!";
        attempt++;
    }
}

这会将您的返回值从isMatch函数保存到名为temp的变量。然后,使用此变量temp可以测试您的状况。

答案 1 :(得分:0)

您的printMatch功能存在许多问题。试一试

void printMatch()
{
    int x1 = 4, y1 = 3, x2 = 2, y1 = 1; //you'll need to define these values in some useful way
    int attempt = 0;

    int match = isMatch(x1, y1, x2, y2);

    if(match == 2)
    {
        cout << "You have a match!";
        attempt++;
    }
}