11 Replies - 1893 Views - Last Post: 15 April 2010 - 02:29 PM Rate Topic: -----

#1 james890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 10

Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 09:00 AM

Hi folks.

I have a text file with a thousands of rows of numbers in it.
For example:

1 2 4 6 11 13 19 21 66 97 101 ........
1 3 9 12 18 26 44 56 98 113......
...........
......

What I want to do is find the maximum number that's in the file.
My thoughts are:

1 - the numbers are all strings, not integers, so I can't easily get the max val.
2 - do I need to use an array to store all the numbers in and calculate the max val from there?
3 - do I need to use itoa to convert the strings into integers?
4 - I need some way of making the program recognise a whole number (i.e. read in the data until you get to a space and then it will know its got a whole number).

Can anyone help me? Is there some fairly straight forward code I can use to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help to get max value from string of numbers in text file

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 09:14 AM

I would use the >> operator from 'istream' class to extract a separate integer from the file. While extracting them, I'd find out the maximum value too. Let me give you a start:
int number; // Individual number
// Extract individual numbers. my_ifstream_object is an instance
// of "ifstream" class (<fstream> header), or directly an instance
// of "fstream".
while (my_ifstream_object >> number)
{
    // number is already converted to an integer, so now,
    // everything you need to do is to implement the algorithm
    // for finding the maximum number.
}


Was This Post Helpful? 0
  • +
  • -

#3 james890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 10

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 10:15 AM

I've tried this:


Code:
while(!datFile.eof() )
{
datFile >> Val;
n = Val;
if (Val > n)
n = Val;
}

and it works.
Problem is I am also wanting to get the number of rows in the file, which I got working already. But I want to get both these values in the same while not end of file block, like this:


Code:
while(!datFile.eof() )
{
datFile >> Val;
n = Val;
if (Val > n)
n = Val;
getline(datFile, line);
m++; 'note that m is the int which holds the row numbers
}
but if I try this, it won't calculate the max value anymore.
Was This Post Helpful? 0
  • +
  • -

#4 james890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 10

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 11:57 AM

Ok, I'm trying this:

	while(datFile >> Val)
	{	
		//datFile >> Val;
		n = Val;
		if (Val > n){
			n = Val;
		}
	}


but I've discovered it's not actually getting the max value in the file.
It's returning 119, but this is not the max value, it's just the last number at the end of the file.
Can you see why it's doing this and what I should do to make it work?
Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 12:11 PM

Look at the code. You are getting a number then you are assigning n to it and comparing it. Val will never be greater than n because they are equal.

You want something like this:
int max = 0;
bool firstRun = true;
while(datFile >> val)
{
  if(firstRun)
  {
    max = val;
    firstRun = false;
  }

  if(val > max)
    max = val;
}

This post has been edited by eker676: 15 April 2010 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 12:14 PM

        while(datFile >> Val) //Read in Val
        {       
                //datFile >> Val;
                n = Val; //Assign n to equal the value stored in Val
                if (Val > n){ // This will never execute because of the above line of code
                        n = Val;
                }
        }



Try removing the first assignment of n= Val;. Leave the one inside the while loop alone.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_james890*


Reputation:

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 01:11 PM

Yes, sorry I forgot to remove that first n = Val.
I have removed it now. And it works.

But how can I get the no of rows as well? Can I not put the code for this in the same bit where I'm calculating the max value? Both won't work together for some reason.

Why won't this work?:

while(datFile >> Val)
{
if (Val > n)
{
n = Val;
}
while(getline(datFile, line))
{
m++;
}
}

'n' now returns as "1" when it should be "150". If I comment out the while getline loop, it DOES return 150.
Was This Post Helpful? 0

#8 Guest_Guest*


Reputation:

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 01:12 PM

Sorry, I forgot to use code tags, so I'll post it again.

	while(datFile >> Val)
	{	
		if (Val > n)
		{
			n = Val;
		}
		while(getline(datFile, line))
		{
			m++;
		}
	}


'n' now returns as "1" when it should be "150". If I comment out the while getline loop, it DOES return 150.
Was This Post Helpful? 0

#9 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 01:27 PM

        while(datFile >> Val)
        {       
                if (Val > n)
                {
                        n = Val;
                }
                while(getline(datFile, line))
/* By calling this inside the other loop you are in effect reading the entire
file before the first while loop gets to read it. So you are really only reading one value for n 
it should be whatever the first value in the file is */
                {
                        m++;
                }
        }



Before the while loop starts you should n = 0;. As far as getting the line count, you won't be able to do it using >> the way you are currently using it. If you want to continue using it this way you can do a .clear() and .seekg() to go back to the beginning of the file, then use the getline() approach I suppose. If you do that, make sure you put the other while loop AFTER your first one finishes.
Was This Post Helpful? 0
  • +
  • -

#10 james890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 10

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 02:02 PM

I have set n=0 before the code that I showed you.

Can you help me with the code?
I've tried this with no luck.

	while(datFile >> Val)
	{	
		if (Val > n)
		{
			n = Val;
		}
			datFile.clear();
			datFile.seekg (0, ios::beg);   'WHAT'S "ios"? I'M NOT USING THIS ANYWHERE	
		
		while(getline(datFile, line))
		{
			m++;
		}
	}

Was This Post Helpful? 0
  • +
  • -

#11 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 02:14 PM

                        datFile.clear();
                        datFile.seekg (0, ios::beg);   'WHAT'S "ios"? I'M NOT USING THIS ANYWHERE       
                
                while(getline(datFile, line))
                {
                        m++;
                }



Remove all of that from your initial while loop and try it. See if it works.
Was This Post Helpful? 0
  • +
  • -

#12 james890  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-April 10

Re: Need help to get max value from string of numbers in text file

Posted 15 April 2010 - 02:29 PM

Haha! I've sorted it.

	while(datFile >> Val)
	{	
		if (Val > n)
		{
			n = Val;
		}
	}

	datFile.clear();
	datFile.seekg(0);
		
	while(getline(datFile, line))
	{
		m++;
	}


Cheer for your help mate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1