-6
#include <tchar.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    int a = (1024^3)-(639^3)-(385^3);
    short max = 0;
    cout<< " Prints 0 if no answer." << endl;

    for(int iii = 100; iii < 1000; iii++)
    {
        if(a % iii = 0)
        {
            max = iii;
        }
    }
    cout<< " " << max;

    cin.clear(); cin.ignore(); cin.get();
    return 0;
}

Just trying to write a simple program so I don't have to calculate and the compiler returns a C2106 error for line 14.

  • 2
    I doubt the compiler only says "C2106 error for line 14". Please provide the **complete** error message, and also add a comment in your code to indicate where is line 14 so we don't have to count. – syam Apr 25 '13 at 11:54
  • 1
    did you intend to use caret (^) as a exponentiation operator? i don't think it works this way in c++ – Vladimir Apr 25 '13 at 11:56

3 Answers3

3
if (a % iii = 0)

This is an invalid assignment. If you meant to compare, use the comparison operator ==.

if (a % iii == 0)

Moreover, exponentiation is not done with ^ in C++. That is actually the bitwise xor operator. To do exponentation, use std::pow from the <cmath> header.

#include <cmath>

int main()
{
    int a = std::pow(1024, 3) - std::pow(639, 3) - std::pow(385, 3);
}
David G
  • 94,763
  • 41
  • 167
  • 253
1

Should be == for comparision

       if((a % iii) == 0)
suspectus
  • 16,548
  • 8
  • 49
  • 57
1

You are using the assignment operator = instead of the equality operator ==:

if(a % iii = 0)

This should be:

if( a % iii == 0)

The result of a % iii is a temporary and and in the original code you are trying to assign 0 to that temporary. Besides that error it looks like you are trying to raise some numbers by a power here:

int a = (1024^3)-(639^3)-(385^3);

the ^ operator is actually bitwise xor what you need is pow from the cmath header:

int a = std::pow(1024,3)-std::pow(639,3)-std::pow(385,3);

I would also avoid using namespace std.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740