0

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;
6
Contributors
7
Replies
9
Views
9 Years
Discussion Span
Last Post by Evan M
0
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...

0

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".

0


should better look like

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.