public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* funny output from g++
@ 2004-05-05  9:39 John Habermann
  2004-05-05 11:14 ` Claudio Bley
  0 siblings, 1 reply; 3+ messages in thread
From: John Habermann @ 2004-05-05  9:39 UTC (permalink / raw)
  To: gcc-help

Hi

I have just been trying to write a program to learn how to use arrays
for my c++ class and I get different output from the program
when it is compiled with g++-2.95, g++-3.2 and g++-3.3. 

I have included the program below but what it does is just take the
following data from a file data.txt:

Bob 17
Sally 23
Bill 13
Wendy 87
Jack 33
Karen 64
Wilbur 79
Betty 42

put that in 2 arrays names[] and ages[] and then I need to find the
maximum age and output the name and age to the screen. 

g++-3.2 returns the correct value 87
g++-2.95 returns 805464728
g++-3.3 returns 268439248

I am running Debian Unstable on a PowerPC and these are just the default
g++ versions from the unstable repositry. 

Sorry if this is a waste of time but wasn't sure why things would be
different. I tries replacing the endl with "\r\n" but the results where
still the same.

Just beginning with programming so was wondering why the different
compilers would be so different.

Thank you for any help

cheers
John

This is the program:


//The aim of this program is to teach us how to use arrays

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

using namespace std;

//declare a global constant for the max number of arrays
const int MAX_ENTRIES = 10;

int main()
{
	//The variables I am getting from the file and putting into an array
	string names[MAX_ENTRIES];
	int ages[MAX_ENTRIES];
	int maxAge;

	//The filestream which i will get the file data from
	ifstream fi("data.txt");

	//Check to see if the file is openned
	if (!fi)
	{
		cout << "error file does not exist" << endl;
		exit(1);
	}

	//Now we need to get the data from the files 
	//Note we use fin.peek to check to see if have reached the end of the
file.	for(int i=0; i < MAX_ENTRIES && fi.peek() !=-1; i++)
	{
		fi >> names[i];
		fi >> ages[i];

//		Just check the variables
		cout << ages[i] << endl;
	}

	//Now to figure out what age is the greatest
	maxAge = 0;
	for(int i=0; i < MAX_ENTRIES; i++)
	{
		if (ages[i] > maxAge)
		{
			maxAge = ages[i];
//			cout << ages[i] << endl;
		}
	}
	cout << maxAge << endl;

	fi.close();
}




-- 
John Habermann

john@ngogeeks.com
http://www.ngogeeks.com
jabberid: jhabbers

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: funny output from g++
  2004-05-05  9:39 funny output from g++ John Habermann
@ 2004-05-05 11:14 ` Claudio Bley
  2004-05-05 11:19   ` Claudio Bley
  0 siblings, 1 reply; 3+ messages in thread
From: Claudio Bley @ 2004-05-05 11:14 UTC (permalink / raw)
  To: gcc-help

On Wed, May 05, 2004 at 07:39:40PM +1000, John Habermann wrote:

Hello John,
 
> I have just been trying to write a program to learn how to use arrays
> for my c++ class and I get different output from the program
> when it is compiled with g++-2.95, g++-3.2 and g++-3.3. 
> 
> I have included the program below but what it does is just take the
> following data from a file data.txt:
> 
> Bob 17
> Sally 23
> Bill 13
> Wendy 87
> Jack 33
> Karen 64
> Wilbur 79
> Betty 42
> 
> put that in 2 arrays names[] and ages[] and then I need to find the
> maximum age and output the name and age to the screen. 
> 
> g++-3.2 returns the correct value 87
> g++-2.95 returns 805464728
> g++-3.3 returns 268439248

The problem with your program is, that your data file only has 8 entries but 
you use all the 10 elements of your array to compute the maximum value.

You have to remember that with local storage usually the memory is not
zero'ed and hence where not explicitely initialised contains random garbage.

G++ 3.2 seems to initialise the arrays to 0 (which it is allowed to I'd say BUT 
it is *not* *required*) or it just happened that the 2 last elements of the 
array were smaller than the maximum value of your data. So, luckily 
it returned the expected value.

	int i = 0; 
	while (i < MAX_ENTRIES && fi >> names[i] && fi >> ages[i]) {
		++i;
	}

 	maxAge = 0;
	for(; i <= 0; --i) {
		// ...
	}

-- 
Claudio

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: funny output from g++
  2004-05-05 11:14 ` Claudio Bley
@ 2004-05-05 11:19   ` Claudio Bley
  0 siblings, 0 replies; 3+ messages in thread
From: Claudio Bley @ 2004-05-05 11:19 UTC (permalink / raw)
  To: gcc-help

On Wed, May 05, 2004 at 01:13:39PM +0200, Claudio Bley wrote:
>  	maxAge = 0;
> 	for(; i <= 0; --i) {
of course, this ^^ should have been ">=" .

-- 
Claudio

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-05-05 11:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-05  9:39 funny output from g++ John Habermann
2004-05-05 11:14 ` Claudio Bley
2004-05-05 11:19   ` Claudio Bley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).