I am converting a console IO program to a file IO program. The program worked perfectly as a console IO. I thought I made the appropriate changes to the program. It compiles, and when I run it output does go to my output text file, but the output is the same as if the input text file was blank. No matter what I do to the input text file the output text file is the same, so somehow my program is not reading it. I know that I spelled the input file name correctly when opening it in the program. Here is the program, thanks for any suggestions.
#include <fstream>
using std::ifstream;
using std::ofstream;
using std::endl;
int main()
{
ifstream inStream;
ofstream outStream;
inStream.open("infile.txt");
outStream.open("output.txt");
int avgRain[12], currentRain[12], x, y, z, r;
outStream << "Please enter the average rainfall (to the nearest inch), starting with January, " << endl;
outStream << "then February, then March, etc. Make sure to press enter after every month." << endl;
for (x = 0; x < 12; x++) // x used to take exactly 12 inputs
{
inStream >> avgRain[x];
outStream << endl;
}
outStream << "What is numeral of the current month? (Jan is 1, Oct is 10, etc)" << endl << endl;
inStream >> y;
outStream << endl;
if (y > 1) // This converts y to variable z. z accounts for the array going from 0 - 11
z = (y - 2);// rather than 1 - 12 and represents the previous month number in the array currentRain
else if (y == 1)
z = 11;
else
z = 10;
outStream << "Please enter the amount of rain (to the nearest inch) that fell " << endl;
outStream << "last month. Then enter the amount that fell the month before " << endl;
outStream << "that. Continue until you have entered an amount for the last 12 " << endl;
outStream << "months. Press enter after every measurement." << endl;
int m = 0;
while (m < 12) // m is used to make sure the while statement is executed exactly 12 times
{
if (z > 0)
{
inStream >> r; // r is the last recorded rain measurement for any month z
outStream << endl;
currentRain[z] = r;
z--;
}
else
{
inStream >> r;
outStream << endl;
currentRain[0] = r;
z = 11;
}
m++;
}
char results = 'g'; // results is the variable that determines how the data is represented
outStream << endl; // and it is initialized to g (graph)
while ((results == 'g') || (results == 'G') || (results == 'c') || (results == 'C'))
{
outStream << "Enter g to see the results graphically or c to see a chart. Or press any " << endl;
outStream << "other key to quit." << endl << endl << endl;
inStream >> results;
if ((results == 'c') || (results == 'C'))
{
outStream << "Month__Average Rainfall__Previous Recorded Rainfall__PRF Deviation From Average" << endl;
x = 0;
while (x < 12)
{
outStream << x + 1;
outStream << " ";
outStream << avgRain[x];
outStream << " ";
outStream << currentRain[x];
outStream << " ";
outStream << currentRain[x] - avgRain[x] << endl;
x++;
}
}
if ((results == 'g') || (results == 'G'))
{
outStream << endl << endl << endl;
x = 0;
while ((x < 12) && (x >= 0))
{
outStream << (x + 1) << " ";
int g = avgRain[x]; // g is initialized to the value of any given x index in
while (g > 0) // the avgRain array
{
outStream << "*";
g--;
}
outStream << endl;
outStream << (x + 1) << " ";
int h = currentRain[x]; // h is similar to g except used for the currentRain array
while (h > 0)
{
outStream << "*";
h--;
}
outStream << endl << endl;
x++;
}
outStream << "The top line indicates the average rainfall; bottom indicates the last " << endl;
outStream << "recorded measure for that month. Every * represents 1 inch." << endl << endl << endl;
}
}
inStream.close();
outStream.close();
}