I have been struggling with this for a while. Will someone please help me?
The issue is within a while loop. The while loop is reading info from getline, copying values from an input file to (1)double array type char[20] , (2)array type double .
The input file is as follows:
first 1.2
second 2.7
third 3.4
fourth 4.2
fifth 2.1
sixth 2.0
The problem code segment:
cout << "test 1 " ;
file_read >> country_name[i][20] >> happiness_values[i];
cout << " test 1.5" << endl;
while (!file_read.getline(line, 100).eof()) && (i < 15) ) // (i < 15) used for
//testing
{
cout << " Test 2 -->" << happiness_values[i] << endl;
total_happiness = happiness_values[i];
i++ ;
file_read >> country_name[i][20] >> happiness_values[i];
}
cout << "test 3" << endl;
my console output:
> a.out
Enter filename: data.dat
data.dat
test 1 test 1.5
Test 2 -->2.18141e-311
Test 2 -->-3.50294e+304
Test 2 -->-8.63291e+304
Test 2 -->5.81427e-312
Test 2 -->-8.50955e+304
Test 2 -->-8.63291e+304
Test 2 -->-8.63291e+304
Test 2 -->3.04209e-277
Test 2 -->0
Test 2 -->0
Test 2 -->0
Test 2 -->-6.20712e+304
Test 2 -->2.11992e-314
Test 2 -->-9.6435e+303
Test 2 -->0
test 3
I have told the program to output only 15 times. When I do not do this the while loop iterates approx 80 times and then i get a segmentation fault error.
I believe the problem to be within the way I am implementing the double array.
I am using the g++ compiler from my schools server so I assume it is the latest version.
On the input file there are no blank lines the end of file.
Thanks.