Array Is Writing Where I Don't Want It To

I have an array that is being populated by this text file:
B,Ben
S,Sean
A,Aaron
D,Dustin


The "snippet" of code that is populating my array is here:
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

void FindCounty() {
   int CountySize=1;
   int CountyNumber;
   string CountyArray[200][1];
   string acronym;
   string name;
   ifstream CountiesFile;

   CountiesFile.open("counties.txt");
   if(!CountiesFile){ 
      cerr << "Error: counties.txt could not be found!\n";
      exit(1);
   }
   while (CountiesFile.good()){
      getline(CountiesFile, acronym, ',');
      CountyArray[CountySize][0]=acronym; 
      getline(CountiesFile, name); 
      CountyArray[CountySize][1]=name;  
      cout << CountySize << ". [" << CountyArray[CountySize][0] << "] " << CountyArray[CountySize][1] << "\n";
      CountySize++; 
      for (int n=0; n<=6; n++){
         cout << CountyArray[n][0] << " " << CountyArray[n][1] << "\n";
      }
   }
   CountiesFile.close(); 
.
.
.


As far as I can tell I'm having B placed at CountyArray[1][0] with Ben being placed at CountyArray[1][1]. I wasn't getting back those same results. The for loop - which is there simply because I wanted to see why I wasn't getting my results - shows me this:
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
1. [B] Ben
 B
B Ben
Ben




2. [S] Sean
 B
B S
S Sean
Sean



3. [A] Aaron
 B
B S
S A
A Aaron
Aaron


4. [D] Dustin
 B
B S
S A
A D
D Dustin
Dustin


I can't see anywhere in my code that I'm telling the array to write to those blocks, and yet it is. Can anyone suggest why it might be?
Last edited on
string CountyArray[200][1];
This is an array of 1 array of 200 strings, so CountyArray[n][1] is out of bounds.
I guess what you want is string CountyArray[200][2];
Last edited on
CountySize should maybe be initialized to 0?
1
2
3
string CountyArray[200][1];
This is an array of 1 array of 200 strings, so CountyArray[n][1] is out of bounds.
I guess what you want is string CountyArray[200][2];


From what I remember, arrays start populating at 0. So an array of [n][1] allows me to write to two places - [n][0] and [n][1] with CountySize=0 being my starting point.

Was I wrong to assume that? I'm starting to think I am, and that I'm remembering it wrong, because I fixed it by making the array string CountyArray[200][2] and writing to CountyArray[n][1] and CountyArray[n][2] with the initial CountySize=1
Last edited on
Also, why would it cause it to have those results in specific? Shouldn't it just not work?
Last edited on
This seems to confirm that I'm right: http://www.cplusplus.com/doc/tutorial/arrays/
Peter87 is correct, when you declare an array the number in the brackets is the size of the array. You declare size of 1 giving you a valid index of 0.
When you declare a multidimensional array, the number in the 'extra' subscript can be interpreted as the number of dimensions.

char array[2][1] ;

is a 2d array, with 1 dimension... it's quite meaningless.

From what I remember, arrays start populating at 0. So an array of [n][1] allows me to write to two places - [n][0] and [n][1] with CountySize=0 being my starting point.


Arrays do begin at 0. And valid indices for an array of a given size N are 0 to N-1. N, in this case, is 1. So the only valid index is 0.

An array of [n][1] size allows you to use [0 to n-1][0] as indices.

Last edited on
Makes sense. I'm actually recalling the class I took a few years back saying the same thing. Thank you all for your inputs.
Topic archived. No new replies allowed.