Access violation problem

Trying to finish this last project so I never have to program again but I can't seem to find the error with this code, I've run the debugger but I can't tell why it crashes at the line it says there is a problem at

The debugger points to line 125 in the second block of code and it says there is an access violation (segmentation fault) but I can't seem to find where the problem is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

// File: StormChaser.cpp
// Programmer: 
// Class: COP 2931
// Date: 
// Description: This program will read hurricane data from a file and produce
//              a series of reports from it.

// Header Files...
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstring>
#include "Storm.h"
using namespace std;

// Constants...
#define MAX_STORMS 200

// Function Prototypes...
Storm* GetStorm(ifstream& input);
void Sort( Storm* List[], int CurStorm );
void DisplayStorms( char *title, Storm List[200], int CurStorm );

int main() {
    // Declarations...
    Storm **cPoint; // Pointer to an object.
    cPoint = new Storm *[200];
    Storm *List[200]; // array of pointers to Storm
    
    
    static int CurStorm = 0;         // Number of Storms in array.
    int Total = 0;           // Number of Storms in input file.
    
    // Temporary Storage Declarations...
    int year;
    int month;
    int day;
    int hour;
    int category;
    std::string name;
    double useless;
    int wind;
    int pressure;
    
    // Opening Data File.
    cout << "Opening data file." << endl;
    ifstream inf("hurricane.data");
    if(inf)
    {
        cout << "File opened successfully..." << endl;
        cout << "Reading file..." << endl << endl;
        }
    else 
    {
	    cout << "File open failure." << endl;
	    return 0;
        }
    // Data file opened.
    
    // Looping GetStorm until there's no more data.
    //while(!inf.eof())
    while(CurStorm < 200)
    {
        cPoint[CurStorm] = GetStorm(inf);
        Total++;
        category = cPoint[CurStorm]->getCategory();
        
        // Saving the Storm if category is > 2.
        if (category > 2)
        {
            List[CurStorm] = cPoint[CurStorm];
            CurStorm++;
            }
        }
   	
	cout << "Number of storms: " << Total << endl;
	cout << "Hurricanes with category 3 and above: " << CurStorm << endl;
	DisplayStorms("First Ten Storms", List[200], 10);
	/*Sort( List, NStorms );
	DisplayStorms( "Top Ten Storms", List, 10 );
	input.close(); */
	
	system("pause");
	return 0;
}

Storm* GetStorm(ifstream& inf) {
    int year, month, day, hour, duration, wind, pressure;
    std::string name;
    double useless;
    
    int category, date, StrLength;
    char charname[15];
    
    // 1. Reading the file.
    inf >> year >> month >> day >> hour >> duration >> name >> useless >> useless >> wind >> pressure;
    
    // Converting date to correct format.
    date = (year * 100000) + (month * 100) + (day);
    
    // Converting name to char array.
    StrLength = name.size();
    for (int i = 0; i < StrLength; i++)
    {    charname[i] = name[i]; }
    
    // Creating Storm Object.
    Storm *cMember;
    cMember = new Storm(date, duration, charname, wind, pressure);
    
    return cMember;
}

void DisplayStorms(char* title, Storm List[], int NStorms ) 
{
    // Display NStorms number of Storms
    // Print the title and column headings; make a loop
    // to print out each Storm.
	cout << title << endl << endl;
	cout << "Begin Date   Duration   Name   Category   Maximum    Minimum" << endl;
	cout << "             (hours)                     Winds (mph) Press. (mb)" << endl;
	cout << "----------------------------------------------------------------" << endl;

	// Print out the first ten Storms in the list
	for (int i = 0; i < 10; i++)
	{
        List[i].print();
    }
	cout << endl << endl;
	return;
}

void Sort( Storm* StormList[], int N ) 
{
	// Selection sort the list of Storm pointers


    int swp_index = 0; //tracks smallest found value
    Storm* temp;
    
    for (int a = 0; a < (N - 1); a++)
    {
        swp_index = a;
        for(int index = a + 1; index < N; index++)
        {
            if (StormList[index]->getCategory() < StormList[swp_index]->getCategory() )
            {
                swp_index = index;
            }
        }
        
        temp = StormList[a];
        StormList[a] = StormList[swp_index];
        StormList[swp_index] = temp;
    }

	return;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// File: Storm.h
// Programmer: 
// Class: COP 2931
// Description: This file defines the Storm Class.

#ifndef Storm_H
#define Storm_H
#endif

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

#define KnotsToMPH 1.15

// global user-defined types:
class Storm {
private:
	int beginDate;
	int duration;
	char name[15];
	int category;
	double wind;
	int pressure;
	void SaffirSimpson();
public:
	Storm( int bdate, int dur, char sname[], double w, int p );
	int getCategory();
	void setDuration( int d );
	void setWind( int w );
	void setPressure( int p );
	void print();
};

Storm::Storm( int bdate, int dur, char sname[], double w, int p )
{
    beginDate = bdate;
    duration = dur;
    name[15] = sname[15];
    wind = w * 1.15;
    pressure = p;
    SaffirSimpson();
    // Start a while loop.
        // Open up the file.
        // Read Year Month Day Hour Category Name Lat Lon Wind.
        // Save into Storm.
        // Move to next piece of data in file.
        // Save next piece in correct storm.
        // Repeat until end marker.
        // Store end marker properly.
        // End while loop.
    // Return to a pointer to a Storm object. Use new to allocate memory.
}

void Storm::setDuration( int d )
{
    duration = d;
}

void Storm::setWind( int w )
{
	// Don't forget to convert windspeed from knots to MPH!
	wind = w * 1.15;
	
}

void Storm::setPressure( int p )
{
    pressure = p;
}

void Storm::SaffirSimpson()
{
	// Compute storm category, using the Saffir-Simpson scale
	if(wind < 34)
	{
		category = -2;  // Tropical Depression
		return;
	}
	if(wind < 64)
	{
		category = -1;  // Tropical Storm
		return;
	}
	if(pressure == 0)
	{
		category = 0;   // Missing pressure
		return;
	}
	if(pressure > 980 && wind < 83)
	{
		category = 1;   // Category 1
		return;
	}
	if(pressure > 965 && wind < 96)
	{
		category = 2;   // Category 2
		return;
	}
	if(pressure > 945 && wind < 113)
	{
		category = 3;   // Category 3
		return;
	}
	if(pressure > 920 && wind < 156)
	{
		category = 4;   // Category 4
		return;
	}
	if(pressure <= 920 && wind >= 156)
	{
		category = 5;   // Category 5
		return;
	}
}

int Storm::getCategory()
{
    return category;
}

void Storm::print()
{
	cout << setw(9) << beginDate << setw(9) << duration << "   "
	     << setw(10) << name << setw(5) << category << setw(10)
		 << wind << setw(12) << pressure << endl; 
	return;
}
You should start out by checking all of the places where you allocate memory (especially line 29) to make sure they don't return NULL. Also, the Storm constructor, on line 40, you can't assign a non-existing array element to another non-existing array element! In other words, sname[ 15 ] doesn't exist in an array of 15 elements; nor does name[ 15 ]! You should either do a character-by-character copy; or a strcpy().
Line 28 is not a pointer, it's a pointer to a pointer. And Line 30 is not an array of type "Storm" it is an array of pointers to type "Storm".
Who was talking about line 28? And in GetStorm(), how do you know the constructor worked on line 110? Don't you need some try/catch blocks?
There's probably a lot more wrong with this file. I'm just pointing out the first things I noticed. The OP told us the error was at Line 125, I noticed that was a comment so I read downward and saw that he was trying to access the ".print()" member function of the Storm class from an array. When I looked to see how the array was created I saw that he did it as an array of pointers. So the access operator on that array will return a pointer since that is what is stored in the array.
Last edited on
Topic archived. No new replies allowed.