Solved

# Write the largest and smallest number in the other file? PP1

Posted on 2005-04-27

Topic. Write a program that will search a file of numbers ot type int and write the largest and the smallest numbesr to the screen. The file contains nothing but numbers of type int separated by blanks or line breaks.

This is my program.

#include <iostream>

#include <fstream>

#include <stdlib.h>

#include <conio.h>

using namespace std;

void max(ifstream& read, int& maximum_number);

void min(ifstream& get, int& minimum_number);

int main()

{

ifstream in_stream;

ofstream out_stream;

in_stream.open("infile_pp1.txt");

if (in_stream.fail( ))

{

cout<<"Input file opening failed.\n";

getch();

exit(1);

}

out_stream.open("outfile_pp1.txt");

if (out_stream.fail( ))

{

cout<<"Output file opening failed.\n";

getch();

exit(1);

}

int maximum_number;

in_stream>>maximum_number;

max(in_stream, maximum_number);

out_stream<<"The largest number is "<<maximum_number<<endl;

int minimum_number;

in_stream>>minimum_number;

min(in_stream, minimum_number);

out_stream<<"The smallest number is "<<minimum_number<<endl;

in_stream.close( );

out_stream.close( );

getch();

system("PAUSE");

return 0;

}

void max(ifstream& read, int& maximum_number)

{

int next;

while (read>>next)

{

if(next>maximum_number)

maximum_number = next;

}

}

void min(ifstream& get, int& minimum_number)

{

int next;

while(get>>next)

{

if(next<minimum_number)

minimum_number=next;

}

}

This program is working find, except they gave the result

The largest number is 25

The smallest number is -2099024616

The largest number is right, but the smallest number is wrong.

This is my infile_pp1.txt

2

5

8

9

17

16

25

Can someone help me to looking for why this is not work with minimum number.