954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

bool function problems!

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;
Shanebear
Newbie Poster
1 post since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

line 26: else should not be capitalized.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

Line 30: return 0; should be inside main function

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 
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...

jencas
Posting Whiz
366 posts since Dec 2007
Reputation Points: 395
Solved Threads: 71
 
and it should not be named "isPrime", because that isn't what it really does...


Hopefully he is not done writing the function.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Evan M
Light Poster
42 posts since Sep 2007
Reputation Points: 11
Solved Threads: 5
 

should better look like

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

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

Evan M
Light Poster
42 posts since Sep 2007
Reputation Points: 11
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You