### I dont know the error for the code

Hey Guys,
I tried to develop a simple Program which finds out a number is prime or composite, but the program always return composite, I dont know what is the problem, can anyone find out what is the error?

//--------------Main.cpp ---------------------
#include<iostream>
#include"declare.h"
#include"define.cpp"

using namespace std;

int main()
{
int number, result;
number = get_num();
result = prime_func(number);
if(result=='1')
{
cout<<"The Entered number is PRIME.\n";
}
else
{
cout<<"The Entered Number is COMPOSITE.\n";
}

return 0;
}

//-------------------------define.cpp----------------
#include<iostream>
#include "declare.h"

using namespace std;

int get_num()
{
int num;
cin>>num;
return num;
}
bool prime_func(int p_num)
{
int div_rem;
for(int i=2; i<=(p_num-1); i++)
{
div_rem = p_num%i;
if(div_rem=='0')
{
return false;
}
}
return true;
}
`if(result=='1')` you're comparing with the literal '1' which is not the number 1, instead the value is 49.

if you use `bool` you should always use `true`/`false` as well.

Please use code tags: [code]`Your code`[/code]
See: http://www.cplusplus.com/articles/jEywvCM9/
Thanks for your comments, coder777 sorry i didnt put my code in tags cause i am new to this website, the problem did not solve yet i think it has mathematical and logical problem but i dont know where?
ne555 if i dont include *.cpp file, the program will get error and will not run.

regards
same mistake here: `if(div_rem=='0')`
again you don't compare with 0 instead the literal '0' (which 48).

Be careful that you don't confuse numbers and literals
Coder777, Thanks for your Help man i got it solved. Thanks so muchhhh for your help ;)
> if i dont include *.cpp file, the program will get error and will not run.
`I've got an error' is not an error message.

<nolyc> Undefined reference is a linker error.
It's not a compile error. #includes don't help. [*]
You did not define the thing in the error message, you forgot to link the file that defines it, you forgot to link to the library that defines it, or, if it's a static library, you have the wrong order on the linker command line.
Check which one. (Note that some linkers call it an unresolved external)

http://www.cplusplus.com/forum/beginner/97938/3/#msg526681
http://www.cplusplus.com/forum/beginner/97938/3/#msg526733

[*] You are not supposed to put function definitions in header files (risk of `multiple definition')
However there are two exceptions: inline and template.
Last edited on
Use code format.
Topic archived. No new replies allowed.