Hi, I'm having an error when running this program and I don't know what kind of error is it. So hoping someone can help me. Thank You!!
Program: The input file is created with positive and negative numbers. When program ask for the input file it should be the same as the input file name that was created.
Output file is used to store the positive numbers only that was read from the input file. Output file name can be called anything user wants. (Doesn't matter what)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void pos_num(ifstream input, ofstream output);
int main()
{
ifstream input;
string inf;
ofstream output;
string outf;
cout << "Enter a name for the input file (ex: val.txt): ";
cin >> inf;
cout << "Enter a name for the output file: ";
cin >> outf;
input.open(inf);
output.open(outf);
if(input.fail() && output.fail())
{
cout << "Couldn't open " << inf << " and " << outf << " file" << endl;
exit(1);
}
pos_num(input, output);
input.close();
output.close();
return 0;
}
void pos_num(ifstream input, ofstream output)
{
int val;
while(input >> val)
{
if(val >= 0)
{
output << val << endl;
}
else;
}
}
Copy & Paste
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;void pos_num(ifstream input, ofstream output);
int main()
{
ifstream input;
string inf;
ofstream output;
string outf;cout << "Enter a name for the input file (ex: val.txt): ";
cin >> inf;
cout << "Enter a name for the output file: ";
cin >> outf;input.open(inf);
output.open(outf);if(input.fail() && output.fail())
{
cout << "Couldn't open " << inf << " and " << outf << " file" << endl;
exit(1);
}pos_num(input, output);
input.close();
output.close();
return 0;
}void pos_num(ifstream input, ofstream output)
{
int val;
while(input >> val)
{
if(val >= 0)
{
output << val << endl;
}
else;
}
}