[RESOLVED] Boolean functions aren't found by main()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Boolean functions aren't found by main()

  1. #1
    Join Date
    Dec 2008
    Posts
    2

    [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);
    	}
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Boolean functions aren't found by main()

    Quote Originally Posted by melikbilge View Post
    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

  3. #3
    Join Date
    Dec 2008
    Posts
    2

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center