Problem with while loop and output files

Hello, I have an assignment for class where I have to write a program to do the following:

1. Read a list of integers inputted by the user (list terminated by entering 0).
2. Prompt for the names of the files to write the integers to, writing positive integers to one file, and negative integers to another.
3. Output the total number of integers in each file to the terminal.

And for the two integer files, there must be exactly five integers per line (excluding the last line) and the each file must end in a new line.


What I have now compiles without any errors, but when i run it with numbers the following errors happen:

1. The first number entered is ignored both in the integer count outputted to the terminal, and in the output file. It doesn't matter whether the first integer is positive or negative.

2. In both output files, each number is outputted on it's own line with a space after it. Instead of there being five integers per line, there is only one. Since there is both a space AND a new line after the integers in each file, it seems like the loop is going through both "if" statements instead of only one?

This is what I have written so far:
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream> 
#include <string>
using namespace std;


int main()
{
  int numPos = 0;        // number of positive intergers
  int numNeg = 0;        // number of negative intergers
  int intInput;          // inputted intergers
  ofstream fout1;        // declare output file stream 1
  string file_name1;     // name of file one
  ofstream fout2;        // declare output file stream 2
  string file_name2;     // name of file two
  int intCounterPosMod = numPos%5; 
// used to determine multiples of five in terms of integer count on a single line for the positive integer file
  int intCounterNegMod = numNeg%5; 
// used to determine multiples of five in terms of integer count on a single line for the negative integer file

    cout << "Enter file name for positive integers: ";
    cin >> file_name1;

    cout << "Enter file name for negative integers: ";
    cin >> file_name2;

    // file_name1.c_str() returns a C style string
    fout1.open(file_name1.c_str(), ios::out);  // open file file_name for output

    if (!fout1.is_open())           // check if file 1 is opened for output
      {
        cerr << "Unable to open file " << file_name1 << endl;
      }
  cout << "Writing to file " << file_name1 << endl;

    // file_name2.c_str() returns a C style string
    fout2.open(file_name2.c_str(), ios::out);  // open file file_name2 for output

    if (!fout2.is_open())           // check if file 2 is opened for output
      {
        cerr << "Unable to open file " << file_name2 << endl;
      }
  cout << "Writing to file " << file_name2 << endl;

  cout << "Enter list of negative and positive integers (followed by 0):" << endl;
  cin >> intInput;

  

  while ((cin >> intInput) && (intInput != 0))
    {
      if ((intInput > 0) && (intCounterPosMod !=0))
	{
	  fout1 << intInput << " ";
	  numPos++;
	}
      else if ((intInput > 0) && (intCounterPosMod == 0))
	{
	  fout1 << intInput << endl;
	  numPos++;
	}
      else if ((intInput < 0) && (intCounterNegMod !=0))
	{
	  fout2 << intInput << " ";
	  numNeg++;
	}
      else if ((intInput < 0) && (intCounterNegMod == 0))
	{
	  fout2 << intInput << endl;
	  numNeg++;
	}
    }

  fout1 << endl;  //both files must end with a new line
  fout2 << endl;  //both files must end with a new line

  cout << file_name1 << " contains " << numPos << " positive integers." << endl;
  cout << file_name2 << " contains " << numNeg << " negative integers." << endl;

  // close file stream fout 1 and 2
  fout1.close();
  fout2.close();

  return 0;
}


Any help would be extremely appreciated. Thanks!
Last edited on
Anybody know what might be the problem? Still not able to figure it out.
Remove line 46 to fix your first problem. You take the first int but then immediately get another one with the start of your while loop.

As for your second problem, it's easiest just to keep a counter of how many numbers you've put into the file, then test
1
2
if (count % 5 == 0)
    cout << endl;

or something like that.
Ah ok. I solved all the errors. Thank you for your help freddy92!
Topic archived. No new replies allowed.