Bool operator help

So I'm trying to write a program and I've stumbled into a ditch. Basically here is the program in a nutshell

1
2
3
4
5
6
7
8
9
int somefunction (int a)
{
Does something
returns true or false
}
main
test some value in the function
if (somefunction(3) == true) // 3 is just an arbitrary number
  cout << "something" << endl;


Is that if statement correct? Can you check if something is true like that or is there a better way to do it? I heard something about ternary operators...

If you want a function to return a Boolean value then you declare the function to do what you intend. Your function currently returns an integer, which can be treated as a Boolean value, 0 is treated as false and !0 is treated as true. I suspect you wanted:

1
2
3
4
5
6
bool somefunction(const int a)
{
   bool ret(false);
   // code to do something and potentially change 'ret' to 'true'
   return ret;
}
Last edited on
Here is my function... is ret the same as return or is it something else?
1
2
3
4
5
6
7
8
9
10
11
bool test_prime(int a)
{
    int i;
    for (i = 0; i < a; i++)
    {
        if (a % i == 0)
            break;
    }
    if (a == i - 1) return (true);
    else return (false);
}
'ret' is just a variable named 'ret'.

Your function is almost fine. The problem is that a == i-1 will never be true because i will never go above a. You probably meant a == i, which would happen if the loop completed without hitting the break line.

The only other thing I would really suggest is get rid of the if/else and just return the condition:

1
2
3
// blech:
    if (a == i) return (true);
    else return (false);

1
2
// better
    return (a == i);



Although I personally find it to be more clear if you just return as soon as you know you'll be returning. So something like this is what I'd do:

1
2
3
4
5
6
7
8
9
bool test_prime(int a)
{
    for (int i = 0; i < a; i++)
    {
        if (a % i == 0)
            return false;  // we know the number isn't prime, so just return false here
    }
    return true;  // otherwise if we made it through the loop, we know it's prime.
}
Last edited on
Thanks.
Now in my main I have the line if (test_prime(5) == true)

This seems to cause my program to crash. Any ideas?
that would not cause your program to crash. At least not with the versions of test_prime that have been posted so far.

You'll have to show more code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

bool test_prime(int a)
{
    int i;
    for (i = 0; i < a; i++)
    {
        if (a % i == 0)
            return false;
    }
    if (a == i) return (true);
}

int main()
{


    if (test_prime(5) == true)

    return 0;
}
Ah, yes that does crash. My mistake.

Although the crash message should have tipped you off what is happening.

if (a % i == 0) // <- when i is zero, this is a division by 0, which is a crash

i must not be zero if you're using it to divide.

Also this is redundant:
if (a == i) return (true);

If the code reaches that line, a will always be == i, so you do not need the if statement there.

What's more... if a somehow is not == i, then your function never hits a return statement, so the output will be undefined (bad!)
Okay, so I switched the redundant line to just return (true);

I also started the for loop at i = 1. The program runs, however there is no output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
using namespace std;

bool test_prime(int a)
{
    int i;
    for (i = 1; i < a; i++)
    {
        if (a % i == 0)
            return false;
    }
    return (true);
}

int main()
{
   /* vector <int> v;
    int x = 1, y, z;
    for (int x; x < 10; x++)
    {
       if (test_prime(x) == false ) cout << "Not prime";

    }*/
    if (test_prime(5) == true)
    cout << "Hello world!" << endl;
    return 0;
}
I also started the for loop at i = 1

That's really a good idea. Otherwise the result of the modulo operation isn't defined. The runtime system should terminate the process in case of the right operand beeing 0 (maybe not the C/C++ system which allows nearly everything - often resulting in much more bulshit results than eveything).
Had to change i = 1 to i = 2. 1 is a multiple of every number so the program thinks the number is not prime already.
Well why don't you just do:

1
2
3
4
bool isPrime(const unsigned int a)
{
   return (a % 2 == 0);
}


That just checks if a number is divisible by two, and if it is it is prime, but if something is divisible by 2 it is not prime and the number can't be divisible by anything other than 1 or itself to be prime.
Topic archived. No new replies allowed.