Help with Error" cannont convert to bool

Oct 28, 2012 at 3:16am
/*
File: isprime.cpp
Created by: ????
Creation Date: ????
Synopsis:
This program reads in a minimum and maximum integer greater than 1
and returns all primes between the minimum and maximum.
*/

#include <iostream>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPE FOR read_range
void read_range(int& imin, int& imax);

// FUNCTION PROTOTYPE FOR is_prime
void is_prime(int& k);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
int imin(0), imax(0);
int prime(0);
cout<<"Enter minimum and maximum:";
cin>>imin,imax;



// Read in range
read_range(imin, imax);

// Print prime numbers

cout << "Primes:";
for (int k = imin; k <= imax; k++)
{
if (is_prime(k))
{
cout << " " << k;
}
}
cout << endl;

return 0;
}

// DEFINE FUNCTION read_range() HERE:
void read_range(int& imax, int& imin)
{
while(imin>imax)
{
cout<<"Error, Minimum must be less then maximum"<<endl;
cout<<"Enter minimum and maximum:";

while((imin<2 || imax<2))
{
cout<<"Error, Minimum and maximum must be at least 2"<<endl;
cout<<"Enter minimum and maximum:";
}
}
}

// DEFINE FUNCTION is_prime() HERE:
void is_prime(int& k)
{
if(k%(k-1)!=0)
cout<<k<<endl;
}


Hello, So i have been working on this code for literally hours and im down to only one error when trying to compile. The exact error is isprime.cpp:39: error: "could not convert `is_prime(((int&)(& k)))' to `bool'".

I understand what the program is supposed to do, it reads in two numbers that the user inputs, first being min and next max. From there i have a void function to ensure two conditions are met, next i have a is_prime function that determines all the numbers that are prime between the min and max number. I believe i have everything correct but i am just lost at what that error means. I am very basic at c++ and this is for one of my home work assignments, i dont need a handout but i am officially begging for help. Thank you so much for looking at this post and thank you even more if you respond.
Oct 28, 2012 at 3:26am
Giving free virtual hugs to anyone that can help me lol
Oct 28, 2012 at 3:33am
Your is_void function returns void, and yet you're trying to use it as a condition for an if statement. Make it return true or false.
Oct 28, 2012 at 3:36am
can you tell me how to make it return true or false like should i put

if(k%(k-1) !=0)
true;
else
false;
?
Oct 28, 2012 at 3:37am
you use "if (is_prime(k))", but is_prime has no return value. change it from void to int or bool and add a return
Oct 28, 2012 at 3:39am
You should read the section on functions to learn how to return something.
http://www.cplusplus.com/doc/tutorial/functions/

Oct 28, 2012 at 3:44am
The problem is i need to make is_prime a void function for this assignement and is_prime(k) is the template the instructor is having us use for this program. I changed the is_prime void function to
{
if(k%(k-1)!=0)
true;
else
false;
}

im so lost with how you two are putting your answers , im really bad at C++. So you can you simplify what your trying to say.
Oct 28, 2012 at 3:50am
We can help solve your C++ problems and answer your questions, but learning C++ is up to you.

You can't return a bool in a void function. Are you sure that you are not allowed to change void is_prime() to bool is_prime()?
Oct 28, 2012 at 3:50am
in my head, the way this function is working is its taking the first number pushing into the if function (is_prime(k)) then jumps to the void function which states that if (k%(k-1)!=0) then its true..... other wise false. It then jumps back up and prints out k as prime. Rinse repeat.

The biggest problem is i cant get Is_prime to actually return true or false which i thought would work with the above code
Oct 28, 2012 at 3:54am
your answer fixed my problem gulshan singh, until you said "bool is_prime()" i have not seen that before. I combed through the chapter on functions in my book and my lecturer never said that either so i guess its just a lack of knowledge. Thanks for the help.
Oct 28, 2012 at 3:54am
I just now have to figure out why its working now but it wont actually print the primes, just blank.
Topic archived. No new replies allowed.