bool checkPrime(int B)
{
for(int i = 2; (i*i) <= B; ++i)
{
returntrue;
if(B % i == 0)
{
returnfalse;
break;
}
}
}
int main()
{
int response;
cout << "Please enter a number between 1 and 50.";
cin >> response;
if (checkInput(response))
{
checkPrime(response);
printResult(checkPrime);
}
}
I didn't include checkInput() and printResult() because both should work, but for some reason checkPrime() always evaluates as true, but if I make a separate program not using a separate bool function and just the put the function into int main() it works. Why does it always evaluate as true? Or could it be something in another function that causes it?
bool checkPrime(int B)
{
for(int i = 2; (i*i) <= B; ++i)
{
if(B % i == 0)
{
returnfalse;
}
}
returntrue;
}
int main()
{
int response;
cout << "Please enter a number between 1 and 50.";
cin >> response;
if (checkInput(response))
{
checkPrime(response);
printResult(checkPrime);
}
}
For some reason it is still always returning true.
int main()
{
int response;
cout << "Please enter a number between 1 and 50.";
cin >> response;
if (checkInput(response))
{
if (checkPrime(response))
printResult();
else
cout << "This number is not prime.";
}
}
Changed printResult to only cout that it's prime rather than be a conditional based on checkPrime, didn't realize the result didn't stay either.