Hi, guys! I'm new to C++ programming and I'm currently using Linux.
I wrote this program(that checks if a number is prime or not):
#include <iostream>
using namespace std;
bool isprime(int);
int main()
{
int num;
do {
cout << "Please enter a number to check whether the number you entered is prime or not(press 0 to stop):\n";
cin >> num;
for ( num = 0;num > 0; num++) {
if (isprime(num) == true) {
cout << num << " is a prime number.\n";
}
else {
cout << num << " is not a prime number.\n";
}
if ( num = 0) {
}
}
} while ( num > 0);
return 0;
}
bool isprime(int x, int y)
{
if ( x < 2 ) {
return false;
}
else if ( x > 2 && ( x % 2) == 0) {
return false;
}
for ( int i = 2; i <= x/2; i++ ) {
if ( x % i == 0) {
return false;
}
}
return true;
}
When I try to compile it with g++,I get this error:In function `main':
perfect.cpp:(.text+0x43):undefined reference to `isprime(int)'
collect2: ld returned 1 exit status.
Please help me find out what's wrong in my program so I can fix the problem.
Thanks in advance!