Hi,
I am having a problem with using file input/output streams in this program.

#include <fstream>
using std::ifstream;
using std::ofstream;
using std::endl;

int main()
{
	ifstream inStream;
	ofstream outStream;

	inStream.open("infile.txt");
	outStream.open("outfile.txt");

	int first, second, third;
	inStream >> first >> second >> third;
	outStream << "The sum of the first3\n"
			  << "numbers in infile.txt\n"
			  << "is " << (first + second + third)
			  << endl;

	inStream.close();
	outStream.close();

	return 0;
}

Every time I create the text file infile.txt, run the program that then creates the text file outfile.txt, it doesn't return to me the sum of the first three numbers in infile.txt like my program tries to do.

Step by Step this is what I do:
1. Create the program.

#include <fstream>
using std::ifstream;
using std::ofstream;
using std::endl;

int main()
{
	ifstream inStream;
	ofstream outStream;

	inStream.open("infile.txt");
	outStream.open("outfile.txt");

	int first, second, third;
	inStream >> first >> second >> third;
	outStream << "The sum of the first3\n"
			  << "numbers in infile.txt\n"
			  << "is " << (first + second + third)
			  << endl;

	inStream.close();
	outStream.close();

	return 0;
}

2. Create the text file infile.txt

right click
create new text document named infile.txt in notepad
input numbers into new text document named infile.txt

3. Run program: which my book says should automatically create the outfile.txt if it is not already created

debug tab
start debugging

4.check if the text file outfile.txt was created and see if the data was added correctly in outfile.txt.

PROBLEM RESULTS:
What has happened is the program creates the an text file named outfile not outfile.txt (though I am assuming that they are the same thing) then outputs all the text the program is supposed to output except that the sum of the numbers in the end is some huge number not the sum of the numbers I placed in the text file infile.txt.

Shows:

The sum of the first3
numbers in infile.txt
is 1717986916 what the heck is this number :(

instead of:

The sum of the first3
numbers in infile.txt
is 5

Would appreciate a very child like explanation cause I am a novice in C++.

ifstreams read from files, not from variables; so this line of code is incorrect:

inStream >> first >> second >> third;

What you're trying to do by that line is to read three int values into the three variables. To make this correct, when you create your file, type in 3 numbers( with a space separating them), then run the code again.

Please post your input file.

I think you are getting the value of an unitialized variable. In c and c++, when you declare a variable like:

int someVariable;

The program is simply reserving an int sized memory block somewhere on the stack. Please note, the program only reserves the spot, it doesn't do anything with the memory. So, whatever data resided there before you declared the variable will become the value of the variable. You have to explicitly set the values of your variables if you want them to be fully intialized:

int someVariable = 0;

I would try this with your variables and run it again. I think you'll see that the huge number is gone.

You should also check the values that you are reading in as a debugging step. This step can be removed later when you are sure that your program is functioning correctly. After you read in the three values, print their values to the screen:

inFile >> var0 >> var1 >> var2;
cout << "var0" << var0 << endl;
cout << "var1" << var1 << endl;
cout << "var2" << var2 << endl;

That way, you can verify what is actually being read by your program. I suspect that whatever is in your input file is being read into none, the first, or the first two variables and at least one of the variables is not getting any input. Add the debugging couts and see what happens.

dusktreader:

Hi,

Well I tried what you said and it did get rid of the huge number but when I set the variables = 0 the outfile data then became

The sum of the first3
numbers in infile.txt
is 0

from:

The sum of the first3
numbers in infile.txt
is 1717986916

it test also showed that:

first: 0
second: 0
third: 0
The sum of the first3
numbers in infile.txt
is 0

using the check the values debugging step you suggested...

Is the program not reading or taking input from my infile.txt text file that I created and just reading the values as regular variables in a program or something?

dusktreader:

Hi,

Well I tried what you said and it did get rid of the huge number but when I set the variables = 0 the outfile data then became

The sum of the first3
numbers in infile.txt
is 0

from:

The sum of the first3
numbers in infile.txt
is 1717986916

it test also showed that:

first: 0
second: 0
third: 0
The sum of the first3
numbers in infile.txt
is 0

using the check the values debugging step you suggested...

Is the program not reading or taking input from my infile.txt text file that I created and just reading the values as regular variables in a program or something?

I figured out the problem so thank you to dusktreader,and tkud for all your help. I must have been writing the file name wrong cause the program would always output some random large number even though I had created a file for the data that the program seemed to not use. Apparently, when I created the file I named it infile.txt instead of infile. When I re-wrote the file name changing it to infile the program took the data from there and output it to the also re-written file name outfile (not outfile.txt).

Thank you again for all your help and I hope to be able to return the favor when I get more adept in programming :)

This question has already been answered. Start a new discussion instead.