Hi,

I am new to C++, for my home work for intro to C++, well it would be easier if I just copy and paste the assignment.

In this program, you will continuously track the minimum and maximum values of a file of integers as the file is read. Your program will:
• read the integers, one by one, from a file. Code should work for a file of any length.
• after reading each integer, it will call a function to determine if a new minimum value or new maximum value has been found
• print the last integer read and the running minimum and maximum values.
• format the output so that the numbers are right aligned.

Do not use the library min/max functions for this program. Instead, write your own function with this prototype:

void minMax (int current, int& min, int& max);

All input and output should be done from your main program, not the function.

Your input file should be named “data.txt” and contain:

54 63 -30 48
12 254 832
-383
22 38 57 1020 19 30 183
-412 38

Your output should look exactly like this:

current is 54, min is 54, max is 54
current is 63, min is 54, max is 63
current is -30, min is -30, max is 63
current is 48, min is -30, max is 63
current is 12, min is -30, max is 63
current is 254, min is -30, max is 254
current is 832, min is -30, max is 832
current is -383, min is -383, max is 832
current is 22, min is -383, max is 832
current is 38, min is -383, max is 832
current is 57, min is -383, max is 832
current is 1020, min is -383, max is 1020
current is 19, min is -383, max is 1020
current is 30, min is -383, max is 1020
current is 183, min is -383, max is 1020
current is -412, min is -412, max is 1020
current is 38, min is -412, max is 1020


This is what I have so far..

#include<iostream>
#include<string>
#include<fstream>

using namespace std;


void minMax(int x, int y, int z,int& min, int& max);

int main()
{

	

		
	float a;
	int b;
	int c;
	int min,max;
	ifstream out;
	out.open("data.txt");
	out>>a;
	b=a;
	c=a;
do
	{
		
	
		cout<<a<<" ";
 minMax(a,b,c,min,max);

		
			
		cout<<min<<" "<<max<<endl;
		
		b=min;
		c=max;
		
		
out>>a;
	}while(out>>a);
	system ("Pause");

	
}

void minMax(int x, int y, int z, int& min, int& max)
{
	if(x<=y && x<=z)
     {
          min=x;
          if(y<z)
               max=z;
          else
               max=y;
     }

     else if (y<=z)
     {
          min=y;
          if(x<z)
               max=z;
          else
               max=x;
     }

     else
     {
          min=z;
          if(x<y)
               max=y;
          else
               max=x;
	
     }

}

it works fine other than the fact that it skips over every other number in the data.txt file. Could anyone help me?

I still need to fix the output but been trying all day to get it to read the file right before I started working on output.

Line 8 is breaking the assignment rules. You need to make the function

void minMax (int current, int& min, int& max);

get rid of x, y, and z.

void minMax (int current, int& min, int& max){

// make comparisons here.
// if current < min, then min = current.
// if current > max, then max = currnet.
}

Then from your main, read in a value from the file, and pass that to the function. You'll have to assign min and max to the first value you read in.

int main(){
//variable declarations
out.open("data.txt");
out>>a;
//initialize high and low to a
do {
minMax (a, high, low);
out>>a;
}while (out>>a)

That's basically what you're looking for. You should be able to fill in the blanks.

this is the new code

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;


void minMax (int current, int& min, int& max);

int main()
{
    int a,b,c;
    int min,max;

    ifstream out;
    out.open("data.txt"); //calls up a file called data.txt
    out>>a;
    
do
    {
        cout<< setw(12)<< "current is "<<setw(8)<<a<<",";

     minMax(a, max, min);

        cout<<setw(12)<<" min is "<<setw(8)<<min<<","<<setw(12)<<" max is "<<setw(8)<<max<<endl;
        
        
        out>>a;
    }while(out>>a);
    system ("Pause");//Using Microsoft Visual C++ 2010 express, to keep output window open for viewing.

    
}

void minMax (int current, int& min, int& max)
{
    if (current<=min && current<=max){
        min=current;

        if (current>max)
                max=current;
        else 
            max=current;
        
    }
}

the output for it is

current is 54, min is -858993460, max is -858993460
current is -30 " "
current is 12 " "

that is why I had the x,y,z into the code. it would not return the right max/min value.

and it is still skipping numbers from the file, next number after 54 is supposed to be 63, then -30, and after -30 it is supposed to be 48 and so on.

Hi Elminster.
I fix0red your c0dez. There was just 4 or 5 lines I had to change. Read the comments in the code.

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

void minMax (int current, int& min, int& max);

int main()
{
    int a;  //removed b and c... they are no longer used
    int min,max;

    ifstream out;
    out.open("data.txt"); 
    out>>a;
	min = a; //have to add these two lines here, otherwise when you call minmax first time...
	max = a; //what are you comparing "a" to??? besides, if there's only 1 number, it's both highest and lowest number.

    
do
    {
        cout<< setw(12)<< "current is "<<setw(8)<<a<<",";

     minMax(a, min, max);//you had min and max reversed here

        cout<<setw(12)<<" min is "<<setw(8)<<min<<","<<setw(12)<<" max is "<<setw(8)<<max<<endl;
        
        //removed line: out >> a.
		//you are already doing it with "while (out>>a)".
		//that is why you were skipping values.
		//out>>a will read 1 number no matter what... putting it as the condition of "while"
		//just says "if the read was successful, continue looping"
		//so basically the while loop reads from the file, until it reaches end.
    }while(out>>a);
    system ("Pause");

    
}

void minMax (int current, int& min, int& max)
{

	//you had a few problems with your if statement
	//I just had to remove some things.. keep it simple.
    if (current < min)
        min=current;

    if (current > max)
        max=current;
       
}

-Greywolf

This question has already been answered. Start a new discussion instead.