I am having trouble detecting the error in this...

I am supposed to write a program that can tell if a positive integer is abundant,deficient, or perfect. But I really don't understand what is wrong here:

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
#include <iostream>
using namespace std;
 
int main()
{
	int number,counter,total;
	
 /* Asks for user input*/	
	cout << "Welcome to the program Factors, this program check to see\n"
		 << "if the number you enter is deficent, perfect or abundant\n"
		 << "this program will read in numbers till you enter -1 and or negitive numbers";
		 
	while (number > -1)
	{

   counter = 1;
	 cout << endl;
	 cout << "Please enter a Integer:\n";
	 cin >> number;
	 cout << endl;
	  
		   
	  if (number % counter == 0)
	  {
	   total= counter + (number % counter);
	   
		cout << total << "+";			   
		counter++;
	  }
	  
	  if (number < total)
	  {
	   cout << endl;
	   cout << number <<" is abundant\n";
	 
	   }
	  else if (number>total)
	  {
	   cout << endl;
	   cout << number << " is deficient\n";
	  
	   cout << endl;
	   }
	   else
	   {
	   cout << endl;
	   cout << number << " is Perfect\n";
	   
	   cout << endl;
	   }
	   
	   }
	  
	 cout<< endl << endl << endl;
	 system ("pause");
	 return 0;
	 }


I there something wrong with my variables? What am I missing?
Last edited on
It is you who should say what is wrong with your program. I do not know what is wrong with your program because I did not write it.
You did not initialize variable number and are trying to compare it with -1

while (number > -1)
Last edited on
How about this:

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>
using namespace std;

int main()
{

//initializing int number as 0

int number = 0,counter,total;

/* Asks for user input*/	
cout << "Welcome to the program Factors, this program check to see\n"
 << "if the number you enter is deficent, perfect or abundant\n"
 << "this program will read in numbers till you enter -1 and or negitive numbers";

	while (number > -1)
	{
		cout << endl;
		cout << "Please enter a Integer:\n";
		cin >> number;
		if (number < 0) { break; }
		cout << endl;

		counter = 1; total = 0;   
		while (counter < number) {
			if (number % counter == 0) { 
				cout << total << "+" << counter;
				total += counter; 
				cout << "=" << total << endl;
			}	
			counter++;
		}

		if (number < total){
			cout << endl;
			cout << number <<" is abundant\n";
		} else if (number>total) {
			cout << endl;
			cout << number << " is deficient\n";

			cout << endl;
		} else {
			cout << endl;
			cout << number << " is Perfect\n";
			cout << endl;
		}

	}

	cout<< endl << endl << endl;

	return 0;
}

Last edited on
And what is the problem?
How about this:


how about counter & total ?

*edit* never mind I see you set the values on line 25.
Last edited on

@SamuelAdams
how about counter & total ?


And what is the problem with counter & total?
cout<< endl << endl << endl;

Why would you put this next to the end of the program? It won't really have any affect.

Other than that nothing seems to be wrong.
ok
Topic archived. No new replies allowed.