Thread: how to get the min & max of a file

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    how to get the min & max of a file

    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:

    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;
            }
    }
    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!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need a variable to remember the current max, and then as you read in each number check to see if it is higher. Do something similar with the min.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM