iamyourmother
iamyourmother

Reputation: 35

Implicit conversion loses integer precision: 'long' to 'int'

I am a beginner of C++. I learnt Python before. When using C++ for programming, my mind always stick to the skills of Python. Here is my question.

int main()
{
   int count ;
   int n1, n2, k, z;
   array<int,8> queen = {1, 7, 4, 6, 4, 5, 0, 4};
   auto array_length = end(queen) - begin(queen);
   count = 0;
   n1 = array_length - 1 ;
   n2 = 1 ;
   while (n1 > 0)
   {
       for (k=0; k < (n1+1) ; k=k+1)
       {
           z = abs(queen[k+n2] - queen[k]);
           if ( z == n2 )
           {
               count += 1 ;
           }
           if ( z == 0 )
               count += 1 ;
           if (( n1 - 1 ) == k)
           {
               n2 += 1 ;
               n1 -= 1 ;

           }
       }
   }
   cout << count << endl;
}

As you see, I have some trouble in line 8 (n1 = array_length - 1 ;). There is a warning telling me that

Implicit conversion loses integer precision: 'long' to 'int'

What's wrong with this? I do appreciate that if you correct my c++ code.

Upvotes: 0

Views: 2024

Answers (1)

Bilal Awan
Bilal Awan

Reputation: 5370

auto array_length = end(queen) - begin(queen);

change it to

int array_length = end(queen) - begin(queen);

auto is picking 'long' automatically. int and long has different sizes. int is 32 bits long is 64 bits

Upvotes: 2

Related Questions