Help with using a while loop.

I need help understanding how I could implement a while loop correctly in my code. I'm supposed to take data from a file (that doesn't involve using fstream but using cin instead) and properly display whether an integer is prime, composite, or neither. I have to display the following:
-the number
-if it is positive (greater than 0), the number of factors it has
-if it is prime, the word "prime"
-if it is composite, the word "composite"
-if it is neither prime nor composite, do not display a factor count or a descriptive term
I must also continue processing integers until the end of file is encountered.
After all numbers have been processed, I have to display the number of values in each category with appropriate labels.
I know I must use an eof controlled while loop, but so far my attempt has been unsuccessful as I get an infinite loop. I have my code displayed below, and I'm positive there's plenty of errors (starting with the loop). Thank you for the assistance.

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 <iomanip>

using namespace std;

int main()
{
  int n;
  int primes = 0;
  int composites = 0;
  int npnc = 0;
  int factors;
  int i;
  bool flag;
int data;

  cout << "NUMBER " << "FACTORS " << "CATEGORY" << endl;
cin >> data;
  while (data)
    {
      if (n < 1)
        {
          npnc++;
          cout << setw(5) << n << endl;
        }
      else
        {
          factors = 0;
          flag = true;

          for(i = 2; i < n; i++)
            {
              if (n % 1 == 0)
 }
            }

          factors = factors + 2;
          if (flag)
            {
              primes++;
              cout << setw(5) << n << " " << setw(5) << factors << " Prime" << endl;
            }
          else
            {
              composites++;
              cout << setw(5) << n << " " << setw(5) << factors << " Composite" << endl;
            }
        }
cin >> data;
    }
  cout << "\nPrimes: " << primes << endl;
  cout << "Composites: " << composites << endl;
  cout << "Neither Prime or Composite: " << npnc << endl;

  return 0;
}

You need to check cin to determine wether there is an error like eof. Since cin has an operator the returns true if there is no error you may write it like so:

while (cin >> data)

Remove line 18/49
Topic archived. No new replies allowed.