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.

p.s. my variable declarations

char country_name[31][20];
 double happiness_values[31];
 double average_happiness = 0;
 double total_happiness = 0;
 char file_name[30];
 int n = 0;
 int i = 0;
 char line[100];\

From my understanding, it's a two-dimensional array.

Given the segmentation fault I do not believe i am implementing it correctly.

Is this correct arrayName[number of cells e.g.0-n]

I'm sorry, but I do not see any connection between my question and your answer.

You wrote the following statement:

file_read >> country_name[20] >> happiness_values;

This statement is equivalent to the following two statements:

file_read >> country_name[20];
file_read >> happiness_values;

I am asking about the first of these two statements.

What were you trying to do when you wrote that statement?

I was trying to get the first block of data into the first cell of country_name[20] so when getline is going through the file it puts "first" into country_names and "1.2" into happiness_values.

Sorry for the misunderstanding. Any help is appreciated.

The phrase "the first cell of country_name[20]" is meaningless, because the expression country_name[20] is not something that has cells.

So I think you need to go back and understand what C++ expressions mean, and what types they have.

What I'm trying to say is that I'm not going to solve your problem for you. I've shown you where it is, and now it's your job to understand what's going on and how to solve it.

From a quick glance i see that I am wrong.

So I will reduce my question to this: If I am going to be reading data (country names) which are sometimes more than 2 words, can i use a single dimensional array or does it have to be a 2d so I can store each different word in a different column.

Depends on what you're trying to do.

If you just write something like

input_file >> var;

then your program will normally stop reading as soon as it finds a space, which means that it will read only a single word no matter what type var has. If, on the other hand, you write

std::string line;
getline(input_file, line);

then it will read an entire line of input into the string named "line", and after that you can do as you like with it.

This article has been dead for over six months. Start a new discussion instead.