I dont know the error for the code

Sep 9, 2013 at 8:05am
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;
cout<<"Please Insert a number: ";
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;
}
Sep 9, 2013 at 10:06am
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/
Sep 9, 2013 at 12:48pm
Sep 9, 2013 at 3:01pm
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
Sep 9, 2013 at 3:08pm
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
Sep 9, 2013 at 3:24pm
Coder777, Thanks for your Help man i got it solved. Thanks so muchhhh for your help ;)
Sep 10, 2013 at 2:53am
> 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 Sep 10, 2013 at 2:56am
Sep 17, 2013 at 1:23pm
Use code format.
Sep 17, 2013 at 3:26pm
Topic archived. No new replies allowed.