U havn't initializsed maximum and minimum numbers variables

U need to initialize them

Amit

U need to initialize them

Amit

Solved

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.

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

if (in_stream.fail( ))

{

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

getch();

exit(1);

}

out_stream.open("outfile_p

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.

6 Comments

You are reading the file into max and min

For max, the file is read and then your max funtion is called

which reads the complete file

Now, the file pointer is at end of file

so, now, when u read file and put it into min, it reads a garbage value which is very large number, so min does not work for your numbers because all your numers happen to be smallr than the garbage

Amit

after the following line

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

-Mahesh

void minmax(ifstream& read, int& minimum_number, int& maximum_number)

{

int next;

while (read>>next)

{

if(next>maximum_number)

maximum_number = next;

if(next < minimum_number)

minimum_number = next;

}

}

You would have to call it like

const int MIN_INT = (-2147483647 - 1);

const int MAX_INT = (2147483647);

int maximum_number = MIN_INT; // init to smallest possible integer (32bits)

int minimum_number = MAX_INT; // init to largest possible integer (32bits)

minmax(in_stream, minimum_number, maximum_number);

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

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

Regards, Alex

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**20** Experts available now in Live!