?
Solved

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

Posted on 2005-04-27
6
Medium Priority
?
1,427 Views
Last Modified: 2008-02-01
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.

                                       
           
0
Comment
Question by:cuong5985
6 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13880879
U havn't initializsed maximum and minimum numbers variables
U need to initialize them

Amit
0
 

Author Comment

by:cuong5985
ID: 13880931
but with that program, max is right, just wrong with min
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13881494
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13881506
You will have to reset the get pointer of your input file

http://cplusplus.com/ref/

Amit
0
 
LVL 4

Expert Comment

by:smpoojary
ID: 13883414
put           in_stream.seekg(0,ios::beg);
after the following line
out_stream<<"The largest number is "<<maximum_number<<endl

-Mahesh
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 200 total points
ID: 13887216
You need to determine min and max using one function.

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


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

807 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question