hello dears!
i have a question about my program i am suposed to make a program that calculates thea factors of a number and add them and see if the number is a perfect number. i made this program and it works fine but now i have to change this program to two funtion program first function, perftest(), shout take an integer an dreturn a bool value of true or false if the argument is or inst a perfect number.

the second function, factorPrint(), takes and integar and prints outthe factors of the argument in ascending order. so this is the function performs the output. and this function should be void.


Now i wrote my code like this, but i get error on the line where i called the both functions on the main() can anyone tell me what is wrogn with my program and what can i do to fix it as soon as possible.

#include <iostream>
using namespace std;
bool perfTest(int Num);
void factorPrint(int Num1);
int  main()
{
 int num;
 int Fact;

 do
 {
  cout << "How Many Numbers Would You Like To Test? ";
  cin >> num;
 }
 while (num<=0);
for(int a=1; a<=num; a++)
{
cout << "Please enter a possible perfect number: ";
cin >> Fact;
if(perfTest()=true) factorPrint(); // This is what i called the functions 
}
 

 return 0;
}
 
// First Function ===============================
 
bool perfTest (int Num)
{
bool perfect;
int B=0;
for(int A=1; A!=Num; A++)
{
 if(Num%A==0)
 {
 B+=A;
 }
}
if(B==Num)perfect=true;
else
perfect=false;
return perfect;
}
 
//second function ===============================
 
void factorPrint(int Num1)
{
for(int A=1; A!=Num1; A++)
{
 if(Num1%A==0)
 {
 cout << Num1 << ":" << A << " ";
 }
}
return;
}

Recommended Answers

All 5 Replies

First of all, remember to post the relevant error messages when you have a problem.

Compiling your program under VC 2003.Net gave me the following error messages.

perfect_nos.cpp(21) : error C2660: 'perfTest' : function does not take 0 arguments
perfect_nos.cpp(22) : error C2660: 'factorPrint' : function does not take 0 arguments

So that means you are not passing the required arguments to the functions. The line numbers where the errors occur are 21 and 22.

if(perfTest()=true)
       factorPrint();

While correcting them, I noticed that you are doing

if(perfTest(Fact)=true)
factorPrint(Fact); // This is what i called the functions

It should be corrected like

if(perfTest(Fact)==true)
factorPrint(Fact); // This is what i called the functions

The full corrected code is as below.

#include <iostream>
using namespace std;
bool perfTest(int Num);
void factorPrint(int Num1);
int  main()
{
    int num;
    int Fact;

    do
    {
        cout << "How Many Numbers Would You Like To Test? ";
        cin >> num;
    }
    while (num<=0);

    for(int a=1; a<=num; a++)
    {
        cout << "Please enter a possible perfect number: ";
        cin >> Fact;
        if(perfTest(Fact)==true) 
            factorPrint(Fact); // This is what i called the functions 
    }
    return 0;
}
 
// First Function ===============================
 
bool perfTest (int Num)
{
    bool perfect;
    int B=0;
    for(int A=1; A!=Num; A++)
    {
        if(Num%A==0)
        {
            B+=A;
        }
    }
    if(B==Num)
        perfect=true;
    else
        perfect=false;
    return perfect;
}
 
//second function ===============================
 
void factorPrint(int Num1)
{
    for(int A=1; A!=Num1; A++)
    {
        if(Num1%A==0)
        {
            cout << Num1 << ":" << A << " ";
        }
    }
    return;
}

Thanks ALOT ALOT for your kind help....

see a small simple error made me think for long time that is what i dont like about programing :)
but still i love it...

I have one more quick question.
in my output when i am typing 0 or bellow zero the program takes forever to give an output. is there anything wrong in my calculation of perfetc and factors

well i i guess i figures it out and it works fine now i just changed the first function from what i had before to this one

bool perfTest (int Num)
{
bool perfect;
int B=0;
if (Num>0)
{
for(int A=1; A!=Num; A++)
{
 if(Num%A==0)
 {
 B+=A;
 }
}
if(B==Num)perfect=true;
}
else
perfect=false;
return perfect;
}

if you can please tell me if what i did is good and logical or just i made the prgram works fine :)

my brain is soo tired i just finished two projects today

so in this case if the number is less than zero it would put the function as false and it will print number is not perfect

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.