[C++] Asigning value to weird place?

Okay, this is something that is just perplexing me.

The concerning code

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
struct global{
  char lines[10][50];
  int nlines;
  float characters[90][2];
  float linew;
  string msgs;
  void defaults(int);

  global(){
    msgs="";
    characters[32][1]=999;
    //defaults();
  }
}g;
//...
int restore(){

  defName(name);
  fstream read(name, ios::in);
  if(!read){
    cout<<"Error: "<<name<<" not found.\nFollow setup process to repair.";
    return 1;
  }
  g.linew=goTo(1);
  g.nlines=goTo(2);
  for(int i=33;i<=90;i++){
    if(i=='#')g.characters['#'][1]=goTo('^', 0, 4); //Get correct # line
    else g.characters[char(i)][1]=goTo(char(i));
  }
  read.close();
  return 0;
}

int f2p(){
  defName(name2);
  fstream read(name2, ios::in);
  if(!read){
    cout<<"Error: "<<name2<<" not found.\nFollow setup process to repair.";
    return 1;
  }
  for(int i=32;i<=90;i++){
    if(i=='#')g.characters['#'][1]=goTo('^'); //Get correct # line
    else g.characters[char(i)][0]=goTo(char(i));
  }
  read.close();
  return 0;
}


Now the weird part is when I run f2p(). This line:
else g.characters[char(i)][0]=goTo(char(i));
for some reason assigns the last value it gets to g.linew. So if it retrieves the numbers
4
5
22.5
564
8
For some very strange reason g.linew is given the value 8.

Then it gets even more perplexing. When I rearange the struct from
1
2
3
4
5
6
7
  float characters[90][2];
  float linew;

//TO

  float linew;
  float characters[90][2];

It no longer does it.

However, with it arranged that way, the program then freezes at a certain (unimportant) part.

Can anybody give me some insight on why this happens?
I'm using Code::Blocks implimented with MSVC++.
Last edited on
you are writing beyond the bounds of the array
for(int i=32;i<=90;i++)
the <= should be a < since the array has 90 elements and indexes "[x]" are 0 based not 1 based like the size
Thanks! That did it!
Nice big reminder on that arrays start at 0 as well. :)
Topic archived. No new replies allowed.