I need help!!!!!!!!

This is a discussion on I need help!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; Why doesn't this work, it gives me a syntax error identifier 'factor', I thought I identified factor. If anyone could ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    I need help!!!!!!!!

    Why doesn't this work, it gives me a syntax error identifier 'factor', I thought I identified factor. If anyone could help me get to where it will compile without errors that would be awesome.

    #include <iostream.h>

    //Factor(n,k) returns true if k is a factor of n and false otherwise

    bool factor(bool n,bool k)

    {
    if (n%k==0)
    return true;
    else
    return false;
    }

    //Prime(n) returns true if the number n is prime and false otherwise
    //Count_factors(k,b,n) counts the number of factors of the number n from a to b inclusive

    bool count_factors(bool k, bool b, bool n)

    {
    if (k>b)
    return 0;
    else
    if factor(n,k)
    return 1+count_factors(k+1,b,n);
    else
    return count_factors(k+1,b,n);
    }

    bool prime (bool n)

    {
    if (count_factors(1,n,n)==2)
    return true;
    else
    return false;
    }

    void main()

    {
    cout << prime(10);
    cout << endl;
    cout << prime(11);
    cout << endl;
    }

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Read the faq and the thread at the top called "Posting Code? Read this first"

    Here is your code nicely formated with code tags and standardized. Note int main, and <iostream> not <iostream.h>.

    Read the FAQ and you will see why. The faq is found at cprogramming.com

    Code:
    #include <iostream>
    using namespace std;
    
    //Factor(n,k) returns true if k is a factor of n and false otherwise
    
    bool factor ( bool n, bool k )
    {
      
    	if ( n % k == 0 )
    		return true;
    
    	else
    		return false;
    	
    }
    
    //Prime(n) returns true if the number n is prime and false otherwise
    //Count_factors(k,b,n) counts the number of factors of the number n from a to b inclusive
    
    bool count_factors ( bool k, bool b, bool n )
    {
    	
    	if ( k > b )
    		return 0;
    
    	else if factor ( n, k )
    		return count_factors ( k + 1 , b, n ) + 1;
    
    	else
    		return count_factors ( k + 1, b, n);
    	
    }
    
    bool prime ( bool n )
    {
    	
    	if ( count_factors ( 1, n, n ) == 2 )
    		return true;
    
    	else
    		return false;
    	
    }
    
    int main()
    {
    	
            cout << prime ( 10 );
    	cout << endl;
    	cout << prime ( 11 );
    	cout << endl;
    
    	return 0;
    	
    }
    I didnt find the problem yet.. let me look and i'll get back to you.

    [edit]
    one thing i noticed

    if ( count_factors ( 1, n, n ) == 2 )

    that wont work..

    count_factors will always return 0 when its done.
    What is C++?

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >else if factor ( n, k )

    if statments need to be put in parenthesis.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by Perspective
    >else if factor ( n, k )

    if statments need to be put in parenthesis.
    Lol, i didnt even see that.
    What is C++?

  5. #5
    Banned
    Join Date
    Aug 2004
    Posts
    128
    were are the int's all i see are bool's
    and if there all bools, how can you check to
    see if one is a factor of the other?

    so every variable up there equals 1 or 0.
    with that said what is the point of

    Code:
    prime(10);
    
    and
    
    prime(11);

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by JarJarBinks
    were are the int's all i see are bool's
    and if there all bools, how can you check to
    see if one is a factor of the other?

    so every variable up there equals 1 or 0.
    with that said what is the point of

    Code:
    prime(10);
    
    and
    
    prime(11);
    Maybe that's why:
    count_factors will always return 0 when its done.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ chasehill:
    your code is very complex, this is my program work as yours:

    Code:
    #include <iostream>
    using namespace std;
    
    int prime(int start, int end)
    {
        int count = 0;
        for(int i = start; i <= end; i++)
        {
           int flag = 0;
           for(int j = 1; j <= i; j++)
            {
                flag += (i % j == 0);
            }
            if (flag == 2 || i == 1)  //is 1 prime?
            { cout << i << endl;
              count++;
            }    
        }
        return count;
    }            
      
    int main()
    {
        int a, b;
        cout << "enter two numbers:";
        cin >> a >>b;
        //cout << prime(a, b) << endl;
        cout<<"there are "<<prime(a, b) << " primes from "
            <<a<<" to "<<b<<endl;
        
        system("pause");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21