Help with a for loop segmentation fault in my code.

Write your question here.
I'm writing a program in c++ which reads an outside weather file with 365 lines (one for each day) in 3 columns. the data is sorted into a string, an int and an int for each row and sorts it's contents into 3 separate arrays. I couldn't use structs, but that part is working. Now I need to find the date of the highest temperature, and I can't seem to get my for loop working correctly. I keep getting a segmentation fault. I also need to compute significant cold fronts, (days 20 degrees (high temp) colder than the previous, the 5 hottest and coldest days, and the 15 median days. I haven't touched those yet, but any advice on how to tackle those would be awesome too. Here's what I got so far:
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
  

#include <iostream>

#include <fstream>

using namespace std;

int find_hottest_day(int high_temps[], int);

int main(){

// set size for arrays

const int SIZE = 365;

// initialized arrays

string dates[SIZE];

int high_temps[SIZE];

int low_temps[SIZE];

int hottest_index;

//open in/out files

ifstream in_file;

ofstream out_file;

in_file.open("weather_data.txt");

out_file.open("weather_results.txt");

int i = 0;

//read file line by line, break if the file doesn't open

while(in_file){

in_file >> dates[i];

in_file >> high_temps[i];

in_file >> low_temps[i];

i++;

if(in_file.fail()){

break;

}

}

//print each line

for(int j = 0; j<SIZE; j++){

cout << dates[j] << " " << high_temps[j] << " " << low_temps[j] << endl;

// out_file << dates[j] << " " << high_temps[j] << " " << low_temps[j] << endl;

}

cout << "the hottest day of the year was" << dates[hottest_index];

}

//This function takes the array values from high temps and the Size as parameters.

//The function will find the index of the hottest day of the year.

//The return value is an integer value which is to be plugged in as an index in the dates[] array.

int find_hottest_day(int high_temps[], const int SIZE){

int hottest_index=0;

for(int k = 0; k < SIZE; k++){

if(high_temps[k] > high_temps[hottest_index]){

hottest_index = k;

}

}

return hottest_index;

}
Line 25: hottest_index is not initialized. Hint: It contains garbage.

Line 69: You subscript into dates[] using hottest_index, which has never been set.

find_hottest_day returns the index to the hottest day, but you never call it.
Think it might be a good idea to call it?


Topic archived. No new replies allowed.