-
December 7th, 2008, 01:57 PM
#1
[RESOLVED] Boolean functions aren't found by main()
I am writing a program that is supposed to find the prime numbers between 1 and 100 using a boolean function. However, when I call the boolean function through int main(), I get the following errors:
error C3861: 'check_prime': identifier not found
error C2365: 'check_prime' : redefinition; previous definition was 'formerly unknown identifier'
The first error points to the function call in main, and the second error points to the curly brace's line after the declaration of the bool function.
Code:
/* File: primes.cpp
* ______________________
* This programs finds the
* prime numbers between 1-100
* using a function.
*/
#include <stdio.h>
#include <math.h>
#include "simpio.h"
#include "genlib.h"
int main()
{
int counter = 0;
bool prime = false;
int divisibleBy;
printf("The prime numbers in the range of 1-100 are:\n");
for (counter = 0; counter <= 100; counter++)
{
prime = check_prime(counter); //error C3861, call to function (check_prime)
if (prime == true)
{
printf("%d\n", counter);
}
else { }
}
}
bool check_prime (int counter)
{ //error C2365 points to this line
int divisibleBy = 0;
int div;
for (div = 1; div <= counter; div++)
{
if (counter % div == 0) divisibleBy++;
}
if (divisibleBy == 2) {
return (true);
}
else {
return (false);
}
}
-
December 7th, 2008, 02:04 PM
#2
Re: Boolean functions aren't found by main()
Originally Posted by melikbilge
I am writing a program that is supposed to find the prime numbers between 1 and 100 using a boolean function. However, when I call the boolean function through int main(), I get the following errors:
In C++, functions must be declared or defined before being called. Either place the entire check_prime() function before main(), or declare it before main().
Code:
bool check_prime (int counter)
{ //error C2365 points to this line
int divisibleBy = 0;
int div;
for (div = 1; div <= counter; div++)
{
if (counter % div == 0) divisibleBy++;
}
if (divisibleBy == 2) {
return (true);
//... whatever
}
int main()
{
// whatever
}
OR
Code:
// declare the function
bool check_prime (int counter) ;
int main()
{
// whatever
}
// function defined here
bool check_prime (int counter)
{ //error C2365 points to this line
int divisibleBy = 0;
int div;
for (div = 1; div <= counter; div++)
{
if (counter % div == 0) divisibleBy++;
}
if (divisibleBy == 2) {
return (true);
//... whatever
}
Regards,
Paul McKenzie
-
December 7th, 2008, 02:26 PM
#3
Re: Boolean functions aren't found by main()
Thank you very much for your help. I tried both solutions and both worked perfectly. Thank you very much! Reputation points promptly added.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|