My assignment is to have an even integer greater than 2 entered and to output two odd prime numbers that add to it. We're supposed to use a bool function but I'm having trouble compiling. I'm getting an error that says "ambiguous overload for operator in cin >> variable"

No idea where I've gone wrong in my code. If you guys can offer any solutions, please make it as simple as possible. Beginner here!

#include <iostream>
using namespace std;

const int variable;
const int i = 1;
const int j = number - 1;

bool isPrime (int)
{
if ( variable > 2 && variable % 2 == 0 )
return true;

else
return false;
}

int main ()
{
cout << "Please enter an even integer greater than 2: " <<
cin >> variable;

If ( isPrime (variable) )
cout << "Two primes that add up to " << variable <<
" are " << i << " and " << j << endl;

Else
cout << "Invalid input!" << endl;
}

return 0;

line 8: you have to declare a variable name inside the parentheses.

line 26: else should not be capitalized.

Line 19: Replace << with semicolon ;
Line 22: "If" should not be capitalized too

Line 30: return 0; should be inside main function

Member Avatar for jencas
bool isPrime (int)
{
if ( variable > 2 && variable % 2 == 0 )
return true;
else
return false;
}

should better look like

bool isPrime (int variable)
{
  return variable > 2 && variable % 2 == 1;
}

and it should not be named "isPrime", because that isn't what it really does...

and it should not be named "isPrime", because that isn't what it really does...

Hopefully he is not done writing the function.

Your problem with the "ambiguous overload for operator in cin >> variable" is caused because variable was declared as const. When you declare something as a const, it cannot be changed. Since a const variable cannot be changed, cin cannot write any data to it.

You'll also have a problem with line 6, because "number" is an undeclared identifier. It doesn't make much sense for i or j to be const anyway. The only output is i and j, and since they are const, the output will always be the same.

You might also want to change the name of "isPrime" to "isEven".


should better look like

bool isPrime (int variable)
{
  return variable > 2 && variable % 2 == 1;
}

That needs to be == 0 instead of == 1 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.