This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
funny output from g++
- From: John Habermann <john dot habermann at jcu dot edu dot au>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 5 May 2004 19:39:40 +1000
- Subject: funny output from g++
- Organization: The Wilderness Society
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