Hi im trying to take a .dat file with just a list of integers separated by lines. The program has to allow for whether or not the list is an even amount of numbers or not. im a bit stuck here is what i have so far.

#include <iostream>
#include <fstream>
using namespace std;

int main( ){
	ifstream input;
	int med_val[100];
	int value, median;
	int count = 1;


	input.open( "asgn.dat" );

	if ( input.fail( ) ){
		cout << "Input file opening failed. \n";

		exit(1);
	}
    

	while ( !input.eof( ) ){
		input >> value;
		count++;
		for ( int i = 0; i < count; i++ ){
		input >> med_val[i];
	   }
	}

	

	if ( ( count % 2 ) == 0 ){
		count = count / 2;

		median = ( ( med_val[ count + 1 ] + med_val[ count ] ) / 2 );
	}
	else {
		count = ( ( count - 1 ) / 2 );
		
		median = med_val[ count ];
	}

        cout << median<< endl;

	return 0;
}

Any help would be appreciated thanks in advance.

The program compiles but it gives me weird output like -858993460 with a the data file containing 1 - 10.

Just what is this section supposed to be doing? Walk through it by hand and analyze what it is REALLy doing.

while ( !input.eof( ) )
{
	input >> value;
	count++;
	for ( int i = 0; i < count; i++ )
        {
	      input >> med_val[i];
	 }
}

The state of your array, when this has run ( with 1...10 as the data file) is
9 10 7 and the rest of the array is uninitialized. Variable count has no real relationship to the data.
Val

well what i want it to do is store the values of the file into an array and add to the counter. ive been trying different ways of doing this so things like this while loop might not make perfect sense. can you explain how to initializ the rest of the array so it can match the count.

Hi im trying to take a .dat file with just a list of integers separated by lines. The program has to allow for whether or not the list is an even amount of numbers or not. im a bit stuck


well what i want it to do is store the values of the file into an array and add to the counter. ive been trying different ways of doing this so things like this while loop might not make perfect sense. can you explain how to initializ the rest of the array so it can match the count.

What you wrote above and the code you posted do not match at all. Your description is very lacking because it's simplistic, confusing, and too much info is missing. The code you wrote requires much more complexity that the above description, too.

Start again and state clearly from beginning to end what you are trying to accomplish. Post additional details that might be necessary, like the input file. And what your expected output should be with the posted input as well as what the output currently is.

OK i am taking a file with data stored like
1
2
3
4
5
then i have to find the median of the numbers in the file. The only condition i have to allow for is that if the amount of numbers in the file are an even number than the median is the average of two numbers.

so what i tried to accomplish in the code was to open the file, read all the values into an array and then count how many numbers there were.

Before you can solve the median value problem, you need to correctly read the data file into your array. See my earlier post. Fix that.

As far as median values go, are you guaranteed that the data file will be in sequential order?
Val

> while ( !input.eof( ) )
Well on the iteration before this becomes true, value is going to get a garbage value (most likely), which is going to completely screw up your attempt at a calculation.

Never use eof() to control a loop, that's not what it's for. while ( input >> value ) is the way to go.

Checking for success in your other file reading functions as well would be a good idea.

thanks so far and yes you are gauranteed that the file is in order.

so far this is what I have i commented out the part that calculates the median but i still cant get the for loop to pass the right integer values into the array.

#include <iostream>
#include <fstream>
using namespace std;

int main( ){
	ifstream input;
	int lineValue[100];
	int value, median;
	int count = 1;


	input.open( "asgn.dat" );

	if ( input.fail( ) ){
		cout << "Input file opening failed. \n";

		exit(1);
	}
    

	while ( input >> value ){
		count++;
	}

	for ( int i = 0; i < count; i++ ){
		input >> lineValue[i];
	}

	/*if ( ( count % 2 ) == 0 ){
		count = count / 2;

		median = ( ( lineValue[ count + 1 ] + lineValue[ count ] ) / 2 );
	}
	else {
		count = ( ( count - 1 ) / 2 );
		
		median = lineValue[ count ];
	}*/
    
	cout << lineValue[1]<< endl;
	cout << count<< endl;

	return 0;
}

> while ( input >> value )
This loops to the end of the file, then stops.

> input >> lineValue;
You're already at the end of the file, so this does nothing.


while ( count < 100 && input >> lineValue[count] ) {
  count++;
}

Now you can do your stats.

Thanks a lot that was really dumb of me, but im new to this stuff so u know problems come up a lot.

here is my final code that functions correctly.

#include <iostream>
#include <fstream>
using namespace std;

int main( ){
	ifstream input;
	double lineValue[100];
	double median;
	int count = 1;


	input.open( "asgn.dat" ); 

	if ( input.fail( ) ){
		cout << "Input file opening failed. \n"; 

		exit(1); 
	}
    

    while ( count < 100 && input >> lineValue[count] ) { 
		count++; 
    }
    
	count = count - 1; 

	if ((count % 2) == 0 ){ 
		count = count / 2;  
        
		median = (( lineValue[ count + 1 ] + lineValue[ count ] ) / 2 ); 
	}
	else {
		count = ( ( count - 1 ) / 2 ) + 1; 
		
		median = lineValue[ count ];
	}
	
	cout << "The median is "<< median<< "."<< endl;

	return 0;
}

If you start count at 0, you won't have to subtract 1 at the end of the loop.

I tried that but then it wasnt reading the right numbers from the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.