M
M
Maxim Rudnev2016-03-11 04:30:35
C++ / C#
Maxim Rudnev, 2016-03-11 04:30:35

Is it possible to apply such a condition?

I have code:

#include <iostream>
#include <conio.h>



using namespace std;
int arr[9];
bool win;

bool proverka()
{
  
  
  if (arr[0] == 2 && arr[1] == 2 && arr[2]==2)
    {
      return true;
      win = true;
    }
    else
    {
      return false;
      win = false;
    }

  
  
}
void output()
{
  for (int i = 0; i < 9; i++)//вывод поля
  {
    
    cout << " | " << arr[i] << " | ";
    if ((i + 1) % 3 == 0)
    {
      
      cout << endl;
    }
  }
}




int main()
{

  setlocale(LC_ALL, "Russian");
  arr[0] = 2;
  arr[1] = 2;
  arr[2] = 2;
  output();
  proverka();
  if (proverka())
  {
    cout << "RAVNO";
  }
  else
  {
    cout << "NE RAVNO";
  }

  _getch();
  return 0;

}

Pay attention to the condition in the proverka() function, everything works correctly only in this form, but I need the condition to be of the form: in
if (arr[0] == arr[1] == arr[2])
this case, the result is "not equal" c and assigning them all the value of one, then everything is OK and a message is displayed about the correct equality. Question: why does comparing array values ​​by index work differently than with ordinary variables? And how can I achieve the desired result?
if (a == b == с)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2016-03-11
@stigmt

if (arr[0] == arr[1] == arr[2])

The result of the == operation in C++ is bool, and bool is converted to int.
The parentheses notation is equivalent to (a[0] == a[1]) == a[2] and compares a[2] with the result of comparing a[0] and a[1] (0 if they are not equal, 1 if they are ). Those. 1 == 1 == 1 is true because (1 == 1) is true, true converted to int is 1, and 1 == 1 is true.
It works exactly the same way.
if (arr[0] == arr[1] && arr[1] == arr[2])

S
Stalker_RED, 2016-03-11
@Stalker_RED

Because it is not clear where you got such syntax from.
Try something like this:if (arr[0] == arr[1] && arr[1] == arr[2])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question