Using TypeDef With Parallel Arrays

I am required to use typedef in order to set up my array types, but it is not working at all. I have a file that has two values, the first being up to a 6 digit number that doesnt start with 0 and the second being the character 'C', 'D', 'E', or 'R'. An example of the file is:
1012 C
3332 R

and so forth. It is known that the file contains 15 lines. I am trying to construct to parallel arrays in order to read all of the data from the file. The first array corresponding to the digits and the second corresponding to the character. I am able to accomplish this without using typedef, but as soon as I do it tells me that the data type is not allowed. Am I not constructing it correctly?

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
typedef int songId[14];
typedef char typeOfSong[14];
int count=0;

while (count < 14 && inFile >> songId[count] >> typeOfSong[count])
{
  count++;
}
for (count = 0; count < 14; count++)
{
  cout << songId[count] << setw(20) << typeOfSong[count] << endl;
}



Am I not constructing it correctly?


You are not. You are treating your typedefs as if they were a normal array.

1
2
3
4
5
6
7
8
9
10
11
12
13
// normal arrays:
int foo[10];  // foo is an array of ints with elements you can access

foo[0] = 5;  // legal, we're assigning 5 to one of those elements.


//...

// what you're doing:
typedef int foo[10];  // this makes 'foo' a TYPE (not an array).  Variables made with this
    // type will be arrays, but 'foo' itself is not an array.

foo[0] = 5;  // illegal.  foo is a typename, not a variable.  This is like saying "int = 6", it's nonsense. 


What you need is a typedef, then use that typedef to create a variable, then assign the variable:

1
2
3
4
5
6
typedef int foo[10];  // foo is the typedef

foo myarray;  // myarray is now an array of 10 ints.
    // Just as if you said "int myarray[10];"

myarray[0] = 5;  // legal 
Last edited on
Thank you! I have now set up my arrays like this:

1
2
3
4
typedef int sizeOfId[15];
typedef char sizeOfType[15];
sizeOfId songId;
sizeOfType typeOfSong;


If I would like to change the second array, typeOfSong, from the characters 'C', 'D', 'E', and 'R' to actual names that represent each character, would I need to use a loop in order to go through the entire array?
Topic archived. No new replies allowed.