cuong5985
asked on
Write the largest and smallest number in the other file? PP1
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_p p1.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.
ASKER
but with that program, max is right, just wrong with min
Oh, i see whats happening
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
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
put in_stream.seekg(0,ios::beg );
after the following line
out_stream<<"The largest number is "<<maximum_number<<endl
-Mahesh
after the following line
out_stream<<"The largest number is "<<maximum_number<<endl
-Mahesh
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
U need to initialize them
Amit