1

This is my first post so if I have written or formated something against forums rules please show some understanding. I also happens to be a novice programmer in C++ so if you could please write somewhat simplified if possible.

I need to read some values from a file named text.txt which is like that (total 13 columns):

1 2 B5 AA 120 0 100 1.5 1.5 G 0 0 0 \n 1 2 B6 AB 120 0 100 1.5 1.5 G 0 0 0 \n 1 2 B7 AC 120 0 100 1.5 1.5 G 0 0 0 \n ... 

The above file can have an undetermined number of lines. It is generated through an other routine.

I made a routine in code::blocks. I have made three matrices holding the above data as integers, doubles, strings. The deceleration I have made is as follows:

int c1, c2, c7, i = 0, nNumPipes; double c5, c6, c8, c9, c11, c12, c13; string c3, c4, c10; int PipeI[][3] = {0}; double PipeD[][6] = {0}; string PipeS[10000][3]; 

Then I use the following syntax to read the lines:

ifstream inf("C:\\text.txt"); while (inf >> c1 >> c2 >> c3 >> c4 >> c5 >> c6 >> c7 >> c8 >> c9 >> c10 >> c11 >> c12 >> c13) { PipeI[i][0] = c1; PipeI[i][1] = c2; PipeS[i][0] = c3; PipeS[i][1] = c4; PipeD[i][2] = c5; PipeI[i][2] = c7; PipeD[i][0] = c8; PipeD[i][1] = c9; PipeS[i][2] = c10; PipeD[i][5] = c11; PipeD[i][3] = c12; PipeD[i][4] = c13; i++; } nNumPipes = i; 

The code however fails in this 'while' loop. Do I miss something?

The error message I get is a window saying "xxx.exe has stop working".

Any help will be appreciated.

3
  • Start small. Break up your input operations so there is only one item read in each one and see which one is failing. Your declarations for PipeI and PipeD` don't make sense. Rather than several snippets you should construct a short complete example. Based on the code you've shown and described it shouldn't be much longer but it might point out subtle issues we can't see now. Commented Apr 5, 2015 at 11:12
  • You are probably better off using c++ standard containers (e.g. std::vector<>) and a struct for each of the data records to read. Commented Apr 5, 2015 at 11:36
  • Also, string PipeS[10000][3] is a rather large amount of memory to allocate on the stack (240kB on my platform). This would be better done dynamically, i.e. using std::vector as πάντα ῥεῖ already mentioned. Commented Apr 5, 2015 at 12:22

1 Answer 1

1

you get this error because your arrays PipeI and PipeD are just one index long in their first brackets so its like

PipeI[1][3]={0}; PipeD[1][6]={0}; 

if you thry to iterate through it you get the errors. Arrays always have fixed size in c++.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.