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++.