-3

im trying to print a certain message according to the return value using 2 different functions. when call the function inside the other is says things are not declared

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++;
    }
}

when i try it like this, it still doesnt work

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++;
    }
}

!!!!!!!!!! UPDATE !!!!!!!!!!!!!!!!!

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;
    }
}
Shay
  • 19
  • 6
  • 1
    What programming language is this?? – AStopher Oct 06 '15 at 20:33
  • The variables `x1`, `y1` etc in `printMatch()` are not defined. – Walter Oct 06 '15 at 20:36
  • [Time to pull out a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), Shay. Your understanding of the basics is weak and needs to be shored up before you get in over your head. – user4581301 Oct 06 '15 at 21:01

2 Answers2

2

Change your printMatch() function to this:

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++;
    }
}

This saves your return value from the isMatch function to a variable named temp. Use can then use this variable temp to test your condition.

Sushant Kafle
  • 446
  • 3
  • 8
0

There are a number of issues with your printMatch function. Give this a try

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++;
    }
}
CollinD
  • 7,304
  • 2
  • 22
  • 45
  • Thank you. With this help i get an error: expected primary-expression before 'int' – Shay Oct 06 '15 at 20:42
  • attempt is for something else in my assignment – Shay Oct 06 '15 at 20:43
  • why do i need to define the values (x1, etc.) if i only need to use the returned value – Shay Oct 06 '15 at 20:52
  • Because you need to pass in values to the function in order for it to work. What do you expect it to compare if you do not provide x1, y1, etc? – CollinD Oct 06 '15 at 20:53
  • i want to print a certain message according to the return value in 'isMatch' . i called the 'IsMatch' funtction in the printMatch to be able to use the return value. does this help you understand? – Shay Oct 06 '15 at 20:56
  • The issue is not my understanding. The issue is that you cannot call the `isMatch` function without four values to utilize in calculations, because that's how the programming language works. – CollinD Oct 06 '15 at 20:57
  • oh mk mk. so can i just set them equal to 0? it wont affect my function will it? – Shay Oct 06 '15 at 20:58
  • It most certainly will. The `isMatch` function in that case will always return 2. It needs to have actual values to use in its comparisons. I think you have deeper issues in your program if these are the kinds of questions coming up. – CollinD Oct 06 '15 at 20:59
  • SUPER SORRY I GOT IT TO WORK. THANK YOU SO MUCH – Shay Oct 06 '15 at 21:00
  • now i have a problem, i dont want my isMatch to always equal 2. If my match = 1 i want it to print something else and it still prints 2. I have updated my question – Shay Oct 06 '15 at 21:14