Hi, I'm a newbie at C++. I am just trying to read a file with three columns of data into arrays and write it back out, to see how arrays work.
This runs with no errors, but the output doesn't look like the input, so I think I just have something specified incorrectly, but not sure what.
My input:
1999 1 3
1999 2 6
1999 4 5
My program:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void OpenFile(ifstream& fin);
void OpenFile(ofstream& fout);
void getInput(ifstream& fin,
int year[],
int month[],
int rain_in_month[]);
int main()
{
using namespace std;
//Variable declarations
const int MONTHS_IN_FILE = 36; //three years of data in file
int year[MONTHS_IN_FILE];
int month[MONTHS_IN_FILE];
int rain[MONTHS_IN_FILE];
ifstream fin; //naming the input file for the program
ofstream fout; //naming the output file for the program
fout.setf(ios::fixed);
fout.setf(ios::showpoint);
fout.precision(1);
//Open the input file
fin.open("rainInput2.txt");
OpenFile(fin);
//Open the output file
fout.open("rainOutput2.txt");
OpenFile(fout);
//Process the records from the input file until the end of file is reached
while (!fin.eof())
{
//Read the input file
getInput(fin, year, month, rain_in_month);
}
for (int i=0; i < MONTHS_IN_FILE; i++)
cout << year[i] << month[i] << rain_in_month[i] << endl;
//Close the input and output files
fin.close();
fout.close();
} // End function main
void OpenFile(ifstream& fin)
{
if (fin.fail())
{
cout<< "Input file opening failed.\n";
exit(1);
}
} // End function OpenFile
void OpenFile(ofstream& fout)
{
if (fout.fail())
{
cout<< "Output file opening failed.\n";
exit(1);
}
} // End function OpenFile
void getInput(ifstream& fin, int year[], int month[], int rain_in_month[])
{
int i = 0;
fin >> year[i] >> month[i] >> rain_in_month[i];
i++;
cout << year[i] << month[i] << rain_in_month[i] << endl;
return;
} // End function getInput