Feb 21, 2016 at 1:37am UTC
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream in_stream;
ifstream in_stream2;
ofstream out_stream;
int num1;
int num2;
in_stream.open("LabInput3-3.txt");
if(in_stream.fail())
{
cout << "File would not open.";
exit(1);
}
in_stream2.open("LabInput3-4.txt");
if(in_stream2.fail())
{
cout << "File would not open.";
exit(1);
}
out_stream.open("LabOutput.txt");
if(out_stream.fail())
{
cout << "File would not open.";
exit(1);
}
while(!in_stream.eof())
{
bool smaller = false;
in_stream >> num1;
while(!in_stream2.eof())
in_stream2 >> num2;
if(num1 < num2)
{
out_stream << num1;
smaller = true;
}
if(smaller == true);
{
out_stream << num2;
}
}
in_stream.close();
in_stream.clear();
in_stream2.close();
in_stream2.clear();
out_stream.close();
return 0;
}
Feb 21, 2016 at 1:55am UTC
Hi,
Sorry, I'm a beginner.
Fixed the semicolon and the loop.
I just put the files in the src folder where the code is, but it still doesn't open.
Feb 21, 2016 at 2:05am UTC
You should be able to do this:
ifstream in_stream("LabInput3-3.txt" );
Also, does the while on line 33 need braces for lines 34 to 43?
Edit: When posting code, post all of it - the include files etc. This is so we can compile it with the cpp.sh gear icon on the top right of the code.
Last edited on Feb 21, 2016 at 2:08am UTC
Feb 21, 2016 at 2:12am UTC
I put braces around the lines and now it builds but instead of displaying "File would not open," the whole program terminates. I checked the output file for any characters but it's empty.
Feb 21, 2016 at 2:23am UTC
Are you using windows? The terminal is probably closing before you see anything, try commenting out the exit statements.
Not sure why the files not opening. I will have a try on my system and see what happens.
Feb 21, 2016 at 2:31am UTC
Oh ok, the output file had some characters OUTSIDE of the src file.
Inside of the 3-3 file is:
6 10 15 22 33 77 88 99
Inside of the 3-4 file is:
30 40 50 60 62 89
but the output file is:
630640650660662689689
How could I change my code so it arranges both the 3-3 and the 3-4 file so it is smallest to largest?
Feb 21, 2016 at 3:32am UTC
Ok, The input files need to be in the same directory as the executable file (my bad earlier), then it should work. I managed to get it working on my system doing this :+)