Problem running file
Oct 24, 2013 at 2:26am UTC
When I run my program, it gets stuck in a loop or something and there are no error messages... any help?
Also, how do you make the program so it doesn't repeat numbers in the out file?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void Mix(const ifstream& file1, const ifstream& file2, ofstream& file3);
int main()
{
int num1, num2;
ifstream file1("file1.txt" );
ifstream file2("file2.txt" );
ofstream file3("file3.txt" );
ifstream in_stream;
ofstream out_stream;
in_stream.open ("file1.txt" );
if (in_stream.fail())
{
cout << "The input file had an opening error.\n" ;
exit(1);
}
out_stream.open ("file3.txt" );
if (out_stream.fail())
{
cout << "The output file had an opening error.\n" ;
exit(1);
}
file1 >> num1;
file2 >> num2;
while (file1 || file2)
{
if (file1 && num1 < num2)
{
file3 << num1 <<endl;
file1 >> num1;
}
else if (file2 && num2 < num1)
{
file3 << num2 <<endl;
file1 >> num2;
}
else if (file1)
{
file1 >> num1;
file3 << num1 <<endl;
}
else
{
file2 >> num2;
file3 << num2 <<endl;
}
}
in_stream.close();
out_stream.close();
}
//Definition of a function
void Mix(const ifstream& file1, const ifstream& file2, ofstream& file3);
Last edited on Oct 24, 2013 at 2:26am UTC
Oct 24, 2013 at 2:45am UTC
Please indent your codes inside if's and while to make it neater:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void Mix(const ifstream& file1, const ifstream& file2, ofstream& file3);
int main()
{
int num1, num2;
ifstream file1("file1.txt" );
ifstream file2("file2.txt" );
ofstream file3("file3.txt" );
ifstream in_stream;
ofstream out_stream;
in_stream.open ("file1.txt" );
if (in_stream.fail())
{
cout << "The input file had an opening error.\n" ;
exit(1);
}
out_stream.open ("file3.txt" );
if (out_stream.fail())
{
cout << "The output file had an opening error.\n" ;
exit(1);
}
file1 >> num1;
file2 >> num2;
while (file1 || file2)
{
if (file1 && num1 < num2)
{
file3 << num1 <<endl;
file1 >> num1;
}
else if (file2 && num2 < num1)
{
file3 << num2 <<endl;
file1 >> num2;
}
else if (file1)
{
file1 >> num1;
file3 << num1 <<endl;
}
else
{
file2 >> num2;
file3 << num2 <<endl;
}
}
in_stream.close();
out_stream.close();
}
//Definition of a function
void Mix(const ifstream& file1, const ifstream& file2, ofstream& file3);
it gets stuck in a loop or something and there are no error messages
Firstly, do not open a file that is already opened:
1 2 3 4 5
ifstream file1("file1.txt" );
//...
ifstream in_stream;
//...
in_stream.open ("file1.txt" ); // you have opened file1.txt already !
Can you say what are you trying to do with the loop so we can clearly identify the problem ?
Last edited on Oct 24, 2013 at 2:46am UTC
Oct 24, 2013 at 2:59am UTC
I switched it so the work is done in the function but I'm trying to make the two input streams merge into the output stream. Each input stream contains a set of integers arranged from smallest to largest. After the program runs, the input streams should be in the third output file arranged in smallest to largest form with no duplicates.. here is my new code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void Mix(ifstream& file1, ifstream& file2, ofstream& file3);
int main()
{
ifstream file1("file1.txt" );
ifstream file2("file2.txt" );
ofstream file3("file3.txt" );
ifstream in_stream;
ofstream out_stream;
in_stream.open ("file1.txt" );
if (in_stream.fail())
{
cout << "The input file had an opening error.\n" ;
exit(1);
}
out_stream.open ("file3.txt" );
if (out_stream.fail())
{
cout << "The output file had an opening error.\n" ;
exit(1);
}
Mix(file1, file2, file3);
in_stream.close();
out_stream.close();
}
//Definition of a function
void Mix(ifstream& file1, ifstream& file2, ofstream& file3)
{
int num1, num2;
file1 >> num1;
file2 >> num2;
while (file1 || file2)
{
if (file1 && num1 < num2)
{
file3 << num1 <<endl;
file1 >> num1;
}
else if (file2 && num2 < num1)
{
file3 << num2 <<endl;
file1 >> num2;
}
else if (file1)
{
file1 >> num1;
file3 << num1 <<endl;
}
else
{
file1 >> num2;
file3 << num2 <<endl;
}
}
}
Oct 24, 2013 at 11:27am UTC
Replace line 50
if (file1 && num1 < num2)
with:
1 2 3 4 5
if (file1 && file2 && num1 == num2) // ignore duplicates
{
file1 >> num1;
}
else if (file1 && num1 < num2)
There are a few errors in the remaining code (reading from wrong file etc.), or out-of-sequence.
Check that num1 is always read from file1 and num2 from file2.
Also check that output to file3 is done
before getting the next number from file1 or file2.
See lines 61, 67, 68, 74 and 75.
Last edited on Oct 24, 2013 at 11:52am UTC
Topic archived. No new replies allowed.