If Else help and showing the letter x

Hello,

im pretty new with c++ and i can only get formating correct but not the if else stuff on where to show an X.

complete a C++ program that will

read a series of integers from a file (via Linux redirection) and display (to the screen) a
table with 4 columns (n, abundant, deficient, perfect)
• for each integer read from the file, display
the integer
an 'X' or a blank in the appropriate column of the table (note: if the integer does
not fall into any of the 3 categories, leave all columns blank)


this is the math that it has to due for each integer.

Given an integer, n, the integers a and b are said to be factors (or divisors) of n if a*b = n.
Given an integer, n, that is greater than 0,
• n is an abundant number if the sum of its factors is greater than 2* n
• n is a deficient number if the sum of its factors is less than 2* n
• n is a perfect number if the sum of its factors is equal to 2* n


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>
#include <cctype>
#include <iomanip>
using namespace std;

int main ()
{
  int data, i = 1, sum = 0;

  cin >> data;
  cout << setw(12) << "Input #" << setw(12) << "Abundant" << setw(12) << "Deficient" <<setw(12) << "Perfect" << endl;
while (data)
{
    
          while (i <= data)
            {
        
              if (data % i == 0)
               sum = sum + i;
        	    i++;
        
            }
    
  char a = 'x';
  char d= 'x';
  char p= 'x';


  if (sum == data * 2)
    {
      p = 'X';
    }
      else if (sum == 0)
        {
          a= ' ';
          d= ' ';
          p= ' ';
        }
          else if (sum > data * 2)
            {
              a = 'X';
            }
              else if (sum < data * 2)
                 {
                  d = 'X';
                 }

  cout << setw(12) << data << setw(12) << a << setw(12) << d <<setw(12) <<  p << endl;
    


    cin >> data;
    
}
 
 
  return 0;
}
Hello sr2cute702,

Two little things I see right off.

Line 8 you initialize "i" and "sum", but why not "data"? Always good practice to initialize your variables.

I the while loop you show no way of ending the loop or the program.

I will have to load up your program and give it a test.

Hope that helps for now,

Andy
Your if else tree is correct but note the else if (sum == 0) condition will never happen because the sum will never be 0, and also you're missing to set the i counter back to 1.

Here's some better formatting.
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
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
	int data, sum, i;
	cout << setw(12) << "Input #"
		 << setw(12) << "Abundant"
		 << setw(12) << "Deficient"
		 << setw(12) << "Perfect"
		 << endl;
	cin >> data;
	while (data) {
		for (sum=0, i = 1; i <= data; ++i) 
			sum += i * (data % i == 0);
		char abundant, deficient, perfect;
		abundant  = (sum >  2*data) ? 'x' : ' ';
		deficient = (sum <  2*data) ? 'x' : ' ';
		perfect   = (sum == 2*data) ? 'x' : ' ';
		cout << setw(6) << data
			 << setw(12) << abundant 
			 << setw(12) << deficient
			 << setw(12) << perfect
			 << endl;
		cin >> data;
	}
	return 0;
}
The program should end when the cin >> data; at line 52 reads an input number which is zero. Then the loop condition while (data) line 12 will be false.

(if there is no zero input, the program will not end).
so i just realized that the file it will end up reading might have -negative number and 0.
Hello sr2cute702,

for lines 24 - 26 change the "x" to a space.

Line 53 reset "i" to 1.

The while loop to figure "sum" is not right. It ends up with the wrong number. Chec Ihatov's for loop for a suggestion.

Hope that helps,

Andy
Thanks Andy, and have a good one!
Topic archived. No new replies allowed.