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();
}

Display your input to the screen when you read it. That way you know if it's being read. If it doesn't display you've found the problem.

you can certainly sprinkle cout statements all over the place to tell you what is happening as the code executes. Then remove them as you fix problems they point out.

Yeah the thing is that I already know it is not reading any input at all from the text document. For example when I cout a variable that was defined by a cin statement, some huge negative number comes out. I just don't know why the program isn't reading my data from my infile text document.

So print out avgRain[x] between lines 21 and 22 (as WaltP was suggesting) check to make sure you're getting something there. You also don't check the status of the file with the is_open() method. It may be that you're trying to open something in a directory outside of the one where the program is executing. Do some tests. Make your data file simple initially, one line or one or two numbers and then substitute in your real one once it's working.

Well, what could possibly stop the variable from being loaded?
Error on read?
Can't get to the read?
Error opening the file?

Did you check for any of these errors? I see no error checking at all.

This article has been dead for over six months. Start a new discussion instead.