Code:
My program compiles perfectly, but I can't get it to work. I'm kind of new at C++ so it's very possible that I'm overlooking something.
Here's my program and a segment from the input file is after the program:
// necessary library header files:
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<string.h>
// global user-defined types:
struct Storm {
int beginDate;
int duration;
char name[15];
int category;
int wind;
int pressure;
};
// function prototypes:
int GetRecord( ifstream &input, int &y,int &m, int &d, int &h,
int &s, char name[], int &wind, int &pres);
Storm* GetStorm( ifstream& input );
int SaffirSimpson( int w );
void Sort( Storm* List[], int NStorms );
void DisplayStorms( const char* title, Storm* List[], int NStorms );
void PrintStorm( Storm* StormPointer );
// Constants
const int MAX_STORMS = 200;
const float KnotsToMPH = 1.15;
int main( void ) {
Storm* List[MAX_STORMS];
Storm* CurrentStorm; // storm returned by GetStorm
int NStorms = 0; // number in aray List
int Total = 0; // total number of storms in the input file
// replace with interactive version
ifstream input("HurricaneTestFile.Data");
// while we have storms to process
while( (CurrentStorm = GetStorm( input ) ) != NULL ) {
++Total;
if( CurrentStorm->category < 3 )
delete CurrentStorm;
else
List[NStorms++] = CurrentStorm;
}
input.close();
cout << "Number of storms: " << Total << endl;
cout << "Hurricanes with category 3 and above: " << NStorms << endl;
DisplayStorms("First Ten Storms", List, 10 );
Sort( List, NStorms);
DisplayStorms("Top Ten Storms", List, 10 );
}
int GetRecord( ifstream &in, int &year, int &month, int &day, int &hour,
int &seq, char name[], int &wind, int &pres ) {
// reads one "record" from the input, and stores in reference arguments
// returns 0 if end-of-file, 1 if record read
float junk;
if (!(in.eof())) {
in >> year >> month >> day >> hour >> seq >> name
>> junk >> junk >> wind >> pres;
return 1;
}
return 0;
}
Storm* GetStorm( ifstream& in ) {
// Build a Storm structure, and return the poointer
// static is necesary to save the last record read for
// the next storm.
static int year = 0, month, day, hour, seq, wind, pressure;
static char name[15];
static status = 1;
int current, cat;
Storm* NewStorm; // we'll return this pointer to the new storm object
double mph;
// If the last storm caused end-of-file, we're done.(return NULL)
if( status == 0 )
return NULL;
// If this is the first time this function is called,
// then read first record.
if( year == 0 )
GetRecord(in, year, month, day, hour, seq, name, wind, pressure);
// Make a storm object and initialize it with info from the current record
NewStorm = new Storm;
NewStorm->beginDate = (year * 1000) + (month * 100) + day;
NewStorm->duration = hour;
strcpy(NewStorm->name, name);
NewStorm->wind = wind;
NewStorm->pressure = pressure;
current = seq;
// Now, keep reading records as long as they go with this storm.
// Watch out for end-of-file!
status = GetRecord(in, year, month, day, hour, seq, name, wind, pressure);
while((strcmp(NewStorm->name, name) == 0) && (status == 1)) {
//update storm info:
NewStorm->duration += 6;
if(NewStorm->wind < wind)
NewStorm->wind = wind;
if(((pressure < NewStorm->pressure) && pressure != 0) || NewStorm->pressure == 0 )
NewStorm->pressure = pressure;
cat = SaffirSimpson(NewStorm->wind);
if(cat > NewStorm->category)
NewStorm->category = cat;
//get next record
GetRecord(in, year, month, day, hour, seq, name, wind, pressure);
}
// convert from knots to mph
NewStorm->wind *= KnotsToMPH;
// and return the pointer to the new storm object:
return NewStorm;
}
void PrintStorm( Storm* ptr ) {
// display one storm
cout << ptr->beginDate << ptr->duration << ptr->name << ptr->category
<< ptr->wind << ptr->pressure << endl;
return;
}
void DisplayStorms( const char* title, Storm* List[], int NStorms ) {
// display NStorms storms
// print some title and column headings; make a loop
// and invoke function "PrintStorm" for each storm.
cout << "Begin Date" << setw(10) << "Duration (hours)" << "Name" << setw(10)
<< "Category" << "Maximum Winds (mph)"
<< "Minimum Pressure (mb)" << endl;
cout << "----------------------------------------------------------------" << endl;
for(int i = 0; i < NStorms; i++)
PrintStorm(*List);
return;
}
int SaffirSimpson( int wind ) {
// Compute storm category, using the Saffir-Simpson scale
int category;
if(wind > 134)
category = 5;
else if ((wind < 134) && (wind > 113))
category = 4;
else if ((wind < 113) && (wind > 96))
category = 3;
else if ((wind < 96) && (wind > 83))
category = 2;
else if ((wind < 83) && (wind > 64))
category = 1;
else if ((wind < 64) && (wind > 34))
category = 'TS';
else
category = 'TD';
return category;
}
void Sort( Storm* x[], int N ) {
// bubble sort the list of Storm pointers
int pass = 0, k, switches;
Storm* temp;
switches = 1;
while( switches ) {
switches = 0;
pass++;
for( k = 0; k < N - pass; k++ ) {
if(x[k]->category < x[k + 1]->category) {
temp = x[k];
x[k] = x[k + 1];
x[k + 1] = temp;
switches = 1;
}
}
}
return;
}
A segment of the input file:
1964 9 20 18 9 GLADYS 29.4 69.7 90 964
1964 9 21 0 9 GLADYS 29.8 69.6 90 964
1964 9 21 6 9 GLADYS 30.4 69.6 85 0
1964 9 21 12 9 GLADYS 31.1 69.8 85 980
1964 9 21 18 9 GLADYS 32.2 70.4 80 977
1964 9 22 0 9 GLADYS 33.1 71.0 80 984
Beginner hu? Not really. If you are, your definately catching on fast. That is FAR from doing math, or Hello World. If no one answers such a long question, try CodeGuru.com, they'll help you out.