Quick question, so I need to get the min and max of a file. The numbers in the file are:
3
5
8
4
and this is my code:
Right now it is just displaying all of the numbers from the file and I'm really kind of stuck... any help is MUCH appreciated!Code:/* Write a program that will search a file of numbers of type int and write the largest and the smallest numbers to the screen. The file contains nothing but numbers of type int separated by blanks or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. */ #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #include <stdlib.h> using namespace std; void make_neat(ifstream& messy_file, ofstream& neat_file, int number_after_decimalpoint, int field_width); int main () { using namespace std; ifstream in_stream; ofstream out_stream; in_stream.open("infile.txt"); out_stream.open("outfile.txt"); if (in_stream.fail( )) { cout << "Input file opening failed.\n"; exit(1); } if (out_stream.fail( )) { cout << "Output file opening failed.\n"; exit(1); } make_neat(in_stream, out_stream, 5, 1); int largest, smallest; in_stream >> largest >> smallest; out_stream << "largest = " << largest << endl; out_stream << "smallest = " << smallest << endl; in_stream.close( ); out_stream.close( ); return 0; } void make_neat(ifstream& messy_file, ofstream& neat_file, int number_after_decimalpoint, int field_width) { neat_file.precision(number_after_decimalpoint); cout.precision(number_after_decimalpoint); double next; while (messy_file >> next) { cout << setw(field_width) << next << endl; neat_file << setw(field_width) << next << endl; } }



LinkBack URL
About LinkBacks


