# Thread: Help with a display issue am having

1. ## Help with a display issue am having

Ok I got an assignment thats to be handed in tonight. I finish the first one and completing the 2nd one now. I have figured out the hard part but a cout statement that i have is not printing in the order i specified.

Question:
The proper divisor of an integer n are the positive divisors less than n. a positive integer is said to be deficient, perfect or abundant number if the sum of its proper divisors are less than, equal to, or greater than the number respectively. For example, 8 is deficient because its proper divisors are 1,2, and 4 and 1 + 2 + 4 < 8; 6 is perfect because 1 + 2 + 3 =6; 12 is abundant because 1 + 2 + 3 +4 +6 > 12. Write a program that classifies n as being deficient, perfect, or abundant for n=20 to 30, then for n=400 to 500 and finally for n=8120 to 8130.

Code:
#include <iostream>

using namespace std;

int n;
char check (int r);

/*
*
*/
int main(int argc, char** argv) {

int r1 = 0, r2, r3;

cout << "Below will be printed the Proper Divisors" << endl;

sleep(5);

for (n = 20; n >= 20 && n <=30; n++) {

cout << "Integer " << n << ":";

for (int d = 1; d < n; d++) {

if (n % d == 0) {

r1 = r1 + d;
cout << d << ", ";
}
}

cout << " = " << r1 << ". It is " << check(r1) << endl;
r1 = 0;
}

return 0;
}

char check (int r) {

if (r < n){

cout << "Deficient";
}

else if (r == n) {

cout << "Perfect";
}

else {
cout << "Abundant";
}
}
Output:
Below will be printed the Proper Divisors
Integer 20:1, 2, 4, 5, 10, Abundant = 22. It is *
Integer 21:1, 3, 7, Deficient = 11. It is *
Integer 22:1, 2, 11, Deficient = 14. It is *
Integer 23:1, Deficient = 1. It is *
Integer 24:1, 2, 3, 4, 6, 8, 12, Abundant = 36. It is *
Integer 25:1, 5, Deficient = 6. It is *
Integer 26:1, 2, 13, Deficient = 16. It is *
Integer 27:1, 3, 9, Deficient = 13. It is *
Integer 28:1, 2, 4, 7, 14, Perfect = 28. It is *
Integer 29:1, Deficient = 1. It is *
Integer 30:1, 2, 3, 5, 6, 10, 15, Abundant = 42. It is
So as you can see the words Abundant, Deficient and Perfect should be printer at the very end, but for some reason it printing else where.  Reply With Quote

2. Hardcore Techie Join Date
Jul 2002
Posts
1,393
Rep Power
0

##   Reply With Quote

3. ## Computer aint old man. Am using NetBeans as my IDE. I also ran the executable file thats created and still get the same output
Last edited by lovepython; Sep 30, 2012 at 05:00 PM.  Reply With Quote

4. Hardcore Techie Join Date
Jul 2002
Posts
1,393
Rep Power
0

## http://ideone.com/ik7yb

I see. That is why I have no confidence in C++ as a language. I may know nothing about programming so far, but C++ based binaries are often problematic to compile on my personal Arch Linux distribution. So, I conclude C++ is absolutely no good.

Plus, I am strongly influenced by things read, like harmful.cat-v.org/software/ [corrected].
Last edited by carey; Sep 30, 2012 at 09:46 PM.  Reply With Quote

5. ## Yes that exactly what am getting. The hamful.cat link not working for me.

The NetBeans IDE uses cygwin as the compiler, which uses gcc and g++ to comiple code.

I find it very strange that it would display something in a place that its not supposed to.  Reply With Quote

6. ## maybe the problem is your check function. you wanted it to return a value but you don't  Reply With Quote

7. Hardcore Techie Join Date
Jul 2002
Posts
1,393
Rep Power
0

## Code:
```Below will be printed the Proper Divisors
Integer 20:1, 2, 4, 5, 10,  = 22. It is Abundant
Integer 21:1, 3, 7,  = 11. It is Deficient
Integer 22:1, 2, 11,  = 14. It is Deficient
Integer 23:1,  = 1. It is Deficient
Integer 24:1, 2, 3, 4, 6, 8, 12,  = 36. It is Abundant
Integer 25:1, 5,  = 6. It is Deficient
Integer 26:1, 2, 13,  = 16. It is Deficient
Integer 27:1, 3, 9,  = 13. It is Deficient
Integer 28:1, 2, 4, 7, 14,  = 28. It is Perfect
Integer 29:1,  = 1. It is Deficient
Integer 30:1, 2, 3, 5, 6, 10, 15,  = 42. It is Abundant```
That's the output from my compiled executable. All the others I tried show the error he is getting.
Code:
```./testcode.cpp:55:1: warning: control reaches end of non-void function
[-Wreturn-type]
}
^
1 warning generated.```
This is the only warning generated.  Reply With Quote

8. ## Ok i finally fixed the problem. It is rather sill/stupid if you ask me but it works.

Changed the single line statement:
cout << " = " << r1 << ". It is " << check(r1) << endl;
To this:
cout << " = " << r1 << ". It is ";
cout << check(r1) << endl;  Reply With Quote

9. Hardcore Techie Join Date
Jul 2002
Posts
1,393
Rep Power
0

## Wow! Compiler turn interpreter now?  Reply With Quote

10. ## So this is now my completed code:

Code:
```/*
* File:   ProperDivisor.cpp
* Author: Dwayne Smith
*
* Created on September 29, 2012, 3:18 PM
*/

#include <iostream>

using namespace std;

int n;
char check (int r);

/*
*
*/
int main(int argc, char** argv) {

int r1 = 0, r2 = 0, r3 = 0;

cout << "Below will be printed the Proper Divisors of a specific range of numbers" << endl << endl;

sleep(5);

cout << "Proper Divisor for range 20 - 30" << endl << endl;

sleep(5);

//Calculate and print Proper divisors and classify
for (n = 20; n >= 20 && n <= 30; n++) {

cout << "Integer " << n << ": ";

for (int d = 1; d < n; d++) {

if (n % d == 0) {

r1 = r1 + d;
cout << d << " ";
}
}

cout << "= " << r1 << ". It is ";
cout << check(r1) << endl;
r1 = 0;
}

//Re-initialize n
n = 0;

cout << endl << "Proper Divisor for range 400 - 500" << endl << endl;

sleep(5);

//Calculate and print Proper divisors and classify
for (n = 400; n >= 400 && n <= 500; n++) {

cout << "Integer " << n << " : ";

for (int d = 1; d < n; d++) {

if (n % d == 0) {

r2 = r2 + d;
cout << d << " ";
}
}

cout << "= " << r2 << ". It is ";
cout << check(r2) << endl;
r2 = 0;
}

//Re-initialize n
n = 0;

cout << endl << "Proper Divisor for range 8120 - 8130" << endl << endl;

sleep(5);

//Calculate and print Proper divisors and classify
for (n = 8120; n >= 8120 && n <= 8130; n++) {

cout << "Integer " << n << " : ";

for (int d = 1; d < n; d++) {

if (n % d == 0) {

r3 = r3 + d;
cout << d << " ";
}
}

cout << "= " << r3 << ". It is ";
cout << check(r3) << endl;
r3 = 0;
}

cin.get();
return 0;
}

char check (int r) {

if (r < n){

cout << "DEFICIENT";
}

else if (r == n) {

cout << "PERFECT";
}

else {
cout << "ABUNDANT";
}
}```
Last edited by Arch_Angel; Oct 1, 2012 at 03:55 AM. Reason: changed quote tag to code tag  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•