Hello! I am having trouble outputting the file "output1.dat" to the screen.
I know that some of my code is wrong but I don't know how to fix it unless I seen the contents of the file i'm sending the results to.
here is my code - thanks for any help you can provide!
/*Precondition: Program must have access to the named file and the
file must contain a list of numbers that are of type 'double.'
Outputs the file name, how many numbers are in the file, and the
average of those numbers to the screen*/
void give_display(ofstream& out_1);
//Connects variable named 'source' to file location
in_1.open("input1.dat");
//If statement monitors the program's ability to access the file
if (in_1.fail())
{
cout << "Could not open in_1 file." << endl;
exit(1);
}
//Connects variable named 'source' to file location
in_2.open("input2.dat");
//If statement monitors the program's ability to access the file
if (in_2.fail())
{
cout << "Could not open in_2 file." << endl;
exit(1);
}
intro ();
out_1.open("output1.dat");
sort_merge(in_1, in_2, out_1);
give_display(out_1);
in_1.close();
in_2.close();
out_1.close();
return 0;
}
//Function definition for what the program does
void intro()
{
cout << "\n This program fetches all the numbers from two files, \n"
<< " sorts the numbers in assending order, then outputs the \n"
<< " list of sorted numbers into a third file." << endl;
}
if you want to know what is inside your output file the easiest way to find out would be to navigate to the file and open it. I would change the files from .dat to .txt while testing to make things easier.
However, if you really want to output the contents of the file to the screen you would first have to close the file. then reopen the file for input. input the data, with a string for example, and then output that to the screen with cout
Thank you for your help!! I was also having trouble with the "sort_merge" function - I sort of had to mickey mouse it together but it works. Sorry about the code tags, I will use them from now on (I would this time but I'm in a hurry, have an appt. to keep):
//Outputs a description of what this program does
void intro();
/*Precondition: must be able to access int numbers from both input files
Postcondition: Puts the merged numbers in ascending order to output file*/
void sort_merge(ifstream& in_1, ifstream& in_2, ofstream& out_1 );
/*Precondition: Program must have access to the named files and the
files must contain lists of numbers that are of type 'int.'
Postcondition: Outputs the file name, how many numbers are in the
file, and the average of those numbers to the screen*/
void give_display(ofstream& out_1);
/*Sets the maximum integer in file 'input1.dat' to 99 in order to start
loop iteration in 'sort_merge' function*/
const int in_1_max = 99;
//Connects variable named 'in_1' to file location
in_1.open("input1.dat");
//If statement monitors the program's ability to access the file
if (in_1.fail())
{
cout << "Could not open 'input1.dat' file." << endl;
exit(1);
}
//Connects variable named 'in_2' to file location
in_2.open("input2.dat");
//If statement monitors the program's ability to access the file
if (in_2.fail())
{
cout << "Could not open 'input2.dat' file." << endl;
exit(1);
}
//Opens the output file so it may be written to
out_1.open("output1.dat");
sort_merge(in_1, in_2, out_1);
give_display(out_1);
in_1.close();
in_2.close();
return 0;
}
//Function definition for what the program does
void intro()
{
cout << "\n This program fetches all the numbers from two files, \n"
<< " sorts the numbers in assending order, then outputs the \n"
<< " list of sorted numbers into a third file." << endl;
}
{
//Variables used as placeholders for
int min_1, min_2;
cout << " \n Here is the complete list of numbers in ascending order:\n";
//Loop iterates as long as either input file has unused values
while (( in_1 >> min_1) || (in_2 >> min_2))
{
/*Sets 'minimum' equal to lowest value in file 'in_1'
and places it in output file*/
if (min_1 != in_1_max)
out_1 << min_1 << endl;
/*Places only one number in output file if the next number compared
in file 'in_2' happens to be equal to the number from 'in_1'*/
if (min_1 == min_2)
out_1 << min_2 << endl;
/*Places any numbers that are greater than those from "'in_1'
to the output file*/
if (min_2 > min_1)
out_1 << min_2 <<endl;
}
}
//Function definition for outputing the results to screen
void give_display(ofstream& out_1)
{
/*Closes 'out_1' as an output file so it that it may be opened as
an input file in order to display contents to the screen*/
out_1.close();
ifstream print;
string line;
//Opens file to print contents to screen
print.open("output1.dat");
if(print.fail())
{
cout << "Could not open 'output1.dat' file.";
exit (1);
}
//Loop iteration allows all numbers to be displayed - each on one line
while(getline(print, line))
{
cout << " \n " << line;
}
print.close();
}