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?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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++;
}
elseif ((intInput > 0) && (intCounterPosMod == 0))
{
fout1 << intInput << endl;
numPos++;
}
elseif ((intInput < 0) && (intCounterNegMod !=0))
{
fout2 << intInput << " ";
numNeg++;
}
elseif ((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;
}