hey there. I am looking for some help. I have been working on this program for days now and for some reason my program will not write to my third file. It will call all of the integers that I need it to call but it will not write them out to a file. also I am unable to figure out how to use my loop correctly. If you could help I'd appreciate it!
#include <iostream>
#include <fstream>
usingnamespace std;
int file1();
int file2();
int file3(int);
int main()
{
int v1;
int v2;
ifstream fin;
do
{
v1 = file1();
v2 = file2();
if (v1 < v2)
{
file3(v1);
}
elseif (v2 < v1)
{
file3(v2);
}
} while (); // not sure how to count the numbers in the file and tell the program my file is empty..
return 0;
}
int file1()
{
int v1;
ifstream fin;
fin.open("file1.txt");
if (fin.fail())
{
cout << "I am unable to open the file file1.txt" << endl;
return 1;
}
fin >> v1;
fin.close();
return v1;
}
int file2()
{
int v2;
ifstream fin;
fin.open("file2.txt");
if (fin.fail())
{
cout << "I am unable to open the file file2.txt" << endl;
return 1;
}
fin >> v2;
fin.close();
return v2;
}
int file3(int num)
{
ifstream fin;
fstream fout;
fin.open("file3.txt");
if (fin.fail())
{
cout << "I am unable to open the file file3.txt" << endl;
return 1;
}
fout << num;
fin.close();
return 0;
}
int file3(int num)
{
ifstream fin;
fstream fout;//better to use ofstream here
//[...]
fout << num; //You have never opened fout...
fin.close();
return 0;
}
according to my professor (unless I interpreted this incorrectly) all you need to do in order to "open fout" as you say is to declare it as ofstream fout;
please tell me otherwise if I am incorrect.
#include <iostream>
#include <fstream>
usingnamespace std;
int file1();
int file2();
int file3(int);
int main()
{
int v1;
int v2;
int lines(1);
int i(0);
cout << "Please enter the number of numbers in your file: ";
cin >> lines;
do
{
v1 = file1();
v2 = file2();
if (v1 < v2)
{
file3(v1);
}
elseif (v2 < v1)
{
file3(v2);
}
i++;
}while (i < lines);
cout << "Your number ordering is complete. Please check file3.txt" << endl;
return 0;
}
int file1()
{
int v1;
ifstream fin;
fin.open("file1.txt");
if (fin.fail())
{
cout << "I am unable to open the file file1.txt" << endl;
return 1;
}
fin >> v1;
fin.close();
return v1;
}
int file2()
{
int v2;
ifstream fin;
fin.open("file2.txt");
if (fin.fail())
{
cout << "I am unable to open the file file2.txt" << endl;
return 1;
}
fin >> v2;
fin.close();
return v2;
}
int file3(int num)
{
ifstream fin;
ofstream fout;
fout.open("file3.txt");
if (fin.fail())
{
cout << "I am unable to open the file file3.txt" << endl;
return 1;
}
fout << num;
fout.close();
return 0;
}
This is the entirety of my code at the moment. I am now writing to file3.txt however I am only writing the first number. There are 5 numbers in each input file. When I have completed the entire program I end up with just a 3 in file3.txt when I should have this:
3 4 5 6 7 8 9 10 13 15
I don't know if you will be able to help me from here on out but I appreciate the help so far. Thank you!
How your code works now:
* open file1.txt, read first number, close file
* open file2.txt, read first number, close file
* find max number
* open file3.txt for writing, erasing everything it contained before, write number, close file
* open file1.txt, read first number, close file (first number will always be the same: you are not deleting it from file)
* open file2.txt, read first number, close file
* find max number
* open file3.txt for writing, erasing everything it contained before, write number, close file (if file reading was working right, you would see only last number in this file)
Could you show me in my code where I am erasing everything it contained before? I had no idea I was doing that. Closing the file doesn't erase everything does it? If so, would this be an appropriate fix to the problem?
int main()
{
ofstream fout;
int v1;
int v2;
int lines(1);
int i(0);
cout << "Please enter the number of numbers in your file: ";
cin >> lines;
do
{
v1 = file1();
v2 = file2();
fout.open("file3.txt");
if (fout.fail())
{
cout << "I am unable to open the file file3.txt" << endl;
return 1;
}
if (v1 < v2)
{
fout << v1;
}
elseif (v2 < v1)
{
fout << v2;
}
i++;
}while (i < lines);
fout.close();
cout << "Your number ordering is complete. Please check file3.txt" << endl;
return 0;
}
This would then get rid of the functionint file3(int);
Could you show me in my code where I am erasing everything it contained before?
There: fout.open("file3.txt");
This is default mode for outputting. You can just open file once and everything will be fine.
If you insist on opening file each time (it is slow and unnessesary load on HDD) you can look at append mode: http://cplusplus.com/reference/fstream/ofstream/open/
Thank you for your help but I got my program to work in a completely different manner. For anyone looking at this program, this was the code I ended up with.