Second Function Doesn't Give a Output

Pages: 12
I'm not sure why my second function isn't giving an output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 #include <iostream>
#include <cmath>
using namespace std;

void isPerfect();
void showPerfect();

int main()
{
 isPerfect();
 showPerfect();
}
void isPerfect ()
{
    int n, sum = 0, f;
cout << "Enter a number: " << endl;
cin >> n;
while (n < 0)
{
cout << "Enter a number: " << endl;
cin >> n;
}

bool result;

for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


if ( sum == n)
{
   result = true;
}

else
{
    result = false;
}

if (result == true)
    {
    cout << "The number " << n << " is a perfect number." << endl;
    }
    else
    {
    cout << "The number " << n << " is not a perfect number." << endl;
    }
}
void showPerfect()
{
        int sum, f, n;
        if (sum == f)
        {
            n = 1;
            cout << f << " = ";

            while (n<f)
            {
                if (f%n == 0)
                    cout << " + " << n;
                n++;
            }
        }
}
line 15 and line 56 - these are local variables within the respective functions. showPerfect() doesn't know what values were calculated for the sum and f variables inside isPerfect().
closed account (E0p9LyTq)
You never pass your variable values from the first function into the second function. Nothing to work on.

Plus all your variables in the second function are undefined, they contain garbage values.

You could solve the problem by making the variables global. Bad idea, though.

Better to rewrite the second function to accept values you have in the first function and call the second while in the first.

Or some other variation. You need to pass the values to the second function.
These lines in function showPerfect() make no sense:

56
57
        int sum, f, n;
        if (sum == f)


sum is not initialised, it contains garbage. The same applies to f.

Then you compare one garbage value with the other garbage value and act on the outcome??



How do I fix this?
Well, I'm not sure that I've seen a description of what the program is supposed to do. Getting it to do something is fairly easy, but it would be better if it was more clear what the assignment or task is intended to achieve.
This is what I have to do:

Perfect Numbers
A positive integer value is said to be "perfect" if it is the sum of its proper divisors; that is, the value is equal to sum of all the numbers (except itself)  dividable by it.
For example, the number 28 is perfect because it is the sum
28 = 1 + 2 + 4 + 7 + 14
Write a program that:

1.     Contains a function isPerfect that determines if a number passed as its argument is perfect,
2.     Contains a second function showPerfect that outputs, using the above format for 28, the equation showing the sum of a number that is already known to be perfect,
3.     Uses the two functions from main to find and show all perfect numbers in a sum format between zero and 8000.

Note 1: the function to determine if the number is perfect should be as efficient as possible. That is, to check for proper divisors, it should only count from 2 to the number whose square is greater than or equal to the number (1 is always a divisor), adding the divisor and its larger counterpart to the sum. For example, for 28, only count to 6, adding 2 & 14 and 4 & 7 along the way.

Note 2: The function to output the sums does not need to (and cannot) be as efficient, since it will only be called a few times. 

Note 3: The body of your main program should include a statement something like this (the loop MUST start at 0):
for (i = 0; i <= 8000; i++)
{
   if (isPerfect(i)) showPerfect(i);
}
 
Thank you. That helps a great deal.

There are some very specific instructions there which give a good clarification about the program logic. In particular, Note 3. Though some of the instructions are rather loosely worded, I would take the phrase "main program" to mean the function main().

In that case, this existing code
5
6
7
8
9
10
11
12
void isPerfect();
void showPerfect();

int main()
{
 isPerfect();
 showPerfect();
}


Would change to look like this:
5
6
7
8
9
10
11
12
13
14
15
bool isPerfect(int n);
void showPerfect(int n);

int main()
{
    for (int i = 0; i <= 8000; i++)
    {
        if (isPerfect(i)) showPerfect(i);
    }
}
 


Notice that the prototypes of the two functions have changed. isPerfect() now returns a bool (true or false) value to show whether or not the number is perfect. Both functions have a a single integer parameter, meaning they deal with checking and printing just one number at a time.

As a consequence a little bit of re-working of your existing code would be required.

That doesn't mean you are limited to just those two functions of course. If you find it necessary or useful, you could use additional 'helper' functions.
What does it mean when this happens?



||=== Build: Debug in Assignment 3 Q2 (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `main':|
C:\Users\mycom\OneDrive\Documents\Assignment 3 Q2\main.cpp|12|undefined reference to `isPerfect(int)'|
C:\Users\mycom\OneDrive\Documents\Assignment 3 Q2\main.cpp|12|undefined reference to `showPerfect(int)'|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
That means, in general that the function named in the message does not exist.

In this particular case, it almost certainly means that you changed the prototypes and you changed main, but you didn't yet change the functions themselves to match. The old versions of the functions will require the function header changing to agree with the prototype. The code inside the functions will require some rework.

Function isperfect is almost correct as it stands. You just need to remove all the cin and cout statements. Instead of getting the number n from cin, use the value passed as the function parameter. Mostly that means removing a few lines of code and tidying up.

Function showPerfect will require a bit more work, but you can borrow most of the code from your existing function, instead of adding the value to the sum, instead output it using cout.



I don't really understand how to rework the code in the showPerfect function. What section of the code do I need to change?
Well, borrowing from the code in the isPerfect function,

You have this:
1
2
3
4
5
6
7
for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


If you change the line
 
    sum += f;
to
 
    cout << " + " << f;
it should give what you need.

Also remove the unnecessary code:
1
2
3
        if (sum == f)
        {
            n = 1;

It isn't needed. Also the matching closing brace } of course.

Perhaps I misled you by suggesting it was more difficult that it really was. Sorry about that.

Last edited on
So this is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 #include <iostream>
#include <cmath>
using namespace std;

bool isPerfect(int n);
void showPerfect(int n);

int main()
{
    for (int i = 0; i <= 8000; i++)
    {
        if (isPerfect(i)) showPerfect(i);
    }
}

bool isPerfect ()
{
    int n, sum = 0, f;
return n;
while (n < 0)
{
return n;
}

bool result;

for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


if ( sum == n)
{
   result = true;
}

else
{
    result = false;
}

if (result == true)
    {
    cout << "The number " << n << " is a perfect number." << endl;
    }
    else
    {
    cout << "The number " << n << " is not a perfect number." << endl;
    }
}
void showPerfect()
{
        int sum, f, n;
        if (sum == f)
       for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      cout << " + " << f;
  }
}
}


But I keep getting the error saying 'undefined reference to' either function
I think there is a misunderstanding, probably unfamiliar terminology.

This is the prototype:
 
bool isPerfect(int n);


... and this is the start of the definition for that function:
16
17
bool isPerfect ()
{


They should match. The parameter int n is missing at line 16.

Now let's backtrack a little.
Take the code from an earlier post, where the function was working - in its own way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void isPerfect ()
{
    int n, sum = 0, f;
cout << "Enter a number: " << endl;
cin >> n;
while (n < 0)
{
cout << "Enter a number: " << endl;
cin >> n;
}

bool result;

for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


if ( sum == n)
{
   result = true;
}

else
{
    result = false;
}

if (result == true)
    {
    cout << "The number " << n << " is a perfect number." << endl;
    }
    else
    {
    cout << "The number " << n << " is not a perfect number." << endl;
    }
}


Now follow these three steps:

1. change the function header.
2. remove all cin and all cout (in this particular function). Instead of cin n, use the parameter n.
3. add a return statement right at the end (function should return a bool value).
4. tidy up if necessary.

This is the outcome after those changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool isPerfect(int n)
{
    int sum = 0, f;

bool result;

for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


if ( sum == n)
{
   result = true;
}

else
{
    result = false;
}

return result;
}


That same code could be written more concisely, but that can wait until another time.
Last edited on
I thought I made the right corrections but I get no output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 #include <iostream>
#include <cmath>
using namespace std;

bool isPerfect(int n);
void showPerfect(int n);

int main()
{
    for (int i = 0; i <= 8000; i++)
    {
        if (isPerfect(i)) showPerfect(i);
    }
}

bool isPerfect(int n)
{
    int sum = 0, f;

bool result;

for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


if ( sum == n)
{
   result = true;
}

else
{
    result = false;
}

return result;
}
void showPerfect(int n)
{
        int sum, f;
        if (sum == f)
       for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      cout << " + " << f;
  }
}
}
As I mentioned previously, in this updated version of showPerfect, this line is not needed
 
    if (sum == f)

The variable sum can be removed from showPerfect.
Do I have the cout in the right place so that it'll ask for one number?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <iostream>
#include <cmath>
using namespace std;

bool isPerfect(int n);
void showPerfect(int n);

int main()
{
    int f;
    cout << "Enter a number: " << endl;
    cin >> f;
    for (int i = 0; i <= 8000; i++)
    {
        if (isPerfect(i)) showPerfect(i);
    }
}

bool isPerfect(int n)
{
    int sum = 0, f;

bool result;

for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      sum += f;
  }
}


if ( sum == n)
{
   result = true;
}

else
{
    result = false;
}

return result;
}
void showPerfect(int n)
{
        int f;
       for( f = 1; f <= n/2; f++)
{
  if ( n % f == 0)
  {
      cout << " + " << f;
  }
}
}
Do I have the cout in the right place so that it'll ask for one number?

At this point I need to re-read the post above where the requirements were stated.
http://www.cplusplus.com/forum/beginner/224333/#msg1026795

Write a program that:

1. Contains a function isPerfect that determines if a number passed as its argument is perfect,
2. Contains a second function showPerfect that outputs, using the above format for 28, the equation showing the sum of a number that is already known to be perfect,
3. Uses the two functions from main to find and show all perfect numbers in a sum format between zero and 8000.


I can't locate the part where the program is to prompt the user to enter a number.


On the other hand, there are still some more things to be done.

1. the output of function showPerfect should be in this format:
28 = 1 + 2 + 4 + 7 + 14

That will require a couple of minor changes to that function - I think you already had some of the code in an earlier version.

2. Refer again to the first note in the assignment:

Note 1: the function to determine if the number is perfect should be as efficient as possible. That is, to check for proper divisors, it should only count from 2 to the number whose square is greater than or equal to the number (1 is always a divisor), adding the divisor and its larger counterpart to the sum. For example, for 28, only count to 6, adding 2 & 14 and 4 & 7 along the way.


That means it isn't enough to simply have a program which works properly and gives the required results. It is asking that the function isPerfect is written in a particular way.
I still get either an error with the showPerfect function or a messed up looking output. I just don't get it.
Well, the latest version of the code above gives this output:
Enter a number: 
 + 1 + 2 + 3 + 1 + 2 + 4 + 7 + 14 + 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 

(user input not shown - I ran this online at the cpp.sh site)

The main body of the output is there, it just needs a newline at the end of each result - a single cout << '\n'; would fix that. And then you need to add the start of the result, which you almost had right at the start of this thread.

The only minor point then is to suppress the "+" output before the first factor (which is always 1).
Pages: 12