error: invalid types ‘unsigned int[unsigned int]’ for array subscript

Feb 11, 2019 at 5:56pm
Why am I getting invalid type on

Project1.cpp:139:32: error: invalid types ‘unsigned int[unsigned int]’ for array subscript
os << Mon[month(date)-1];


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
  void displayDate(const Date& date, ostream& os, DATE_STYLE ds)
{

 // if ds == MM_DD_YYYY)

  if (!wellFormed(date))
  {
     cout << "Date Error\n";
     exit(1);
  }
  else
    if (ds == MM_DD_YYYY)
    {
      if (numDigits(date) == 7)
        os << '0';
        os << month(date) << '/';
        if (nthDigit(date, 5) == 0)
           os << '0';
           os << day(date) << '/';
           unsigned y = year(date);
           unsigned len = 4 - numDigits(y);
           for (unsigned i = 0; i < len; ++i)
             os << '0';
             os << year(date) << endl;
     }
     else
       string Mon[12] = {"Jan ", "Feb ", "Mar ", "Apr ", "May ", "June ", "Jul ", "Aug ",
       "Sept ", "Oct ", "Nov ", "Dec " };
        unsigned Mon;
        os << Mon[month(date)-1];
        if (nthDigit(date, 5) == 0)
        os << '0';
        os << day(date) << ",";
        unsigned y = year(date);
        unsigned len = 4 - numDigits(y);
        for (unsigned i = 0; i < len; ++i)
        {
          os << '0';
          os << year(date) << endl;
        }
}
Feb 11, 2019 at 6:11pm
what does month(date)-1 give you?
is it an array or an int?
Last edited on Feb 11, 2019 at 6:11pm
Feb 11, 2019 at 6:52pm
date is between 1010001 and 12319999
Feb 11, 2019 at 8:24pm
what does the month function return (its type)?
Feb 11, 2019 at 8:29pm
Look at the first error message (the one before the one you posted).
Last edited on Feb 12, 2019 at 7:43am
Feb 12, 2019 at 7:20am
Line 27 is within the else clause. Everything from line 29 on is outside the else clause. Hence Mon does not exists on line 30.

You need to use {} in order to define the blocks. Your indention is misleading.
Feb 12, 2019 at 11:01am
Hello thishas,

Reefer to post http://www.cplusplus.com/forum/beginner/249707/ and message http://www.cplusplus.com/forum/beginner/249707/#msg1099850

Now why does your code here look the same as the previous post? If you are not going to change anything how do you expect to improve?

Here is your code properly indented with some blank lines to better show what it is doing:
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
48
49
50
void displayDate(const Date& date, ostream& os, DATE_STYLE ds)
{
	// if ds == MM_DD_YYYY)

	if (!wellFormed(date))
	{
		cout << "Date Error\n";
		exit(1);
	}
	else
		if (ds == MM_DD_YYYY)
		{
			if (numDigits(date) == 7)
				os << '0';

			os << month(date) << '/';

			if (nthDigit(date, 5) == 0)
				os << '0';

			os << day(date) << '/';

			unsigned y = year(date);
			unsigned len = 4 - numDigits(y);

			for (unsigned i = 0; i < len; ++1)
				os << '0';

			os << year(date) << endl;
		}
		else
			string Mon[12] = { "Jan ", "Feb ", "Mar ", "Apr ", "May ", "June ", "Jul ", "Aug ",
			"Sept ", "Oct ", "Nov ", "Dec " };

	os << Mon[month(date) - 1];

	if (nthDigit(date, 5) == 0)
		os << '0';

	os << day(date) << ",";

	unsigned y = year(date);
	unsigned len = 4 - numDigits(y);

	for (unsigned i = 0; i < len; ++1)
		os << '0';

	os << year(date) << endl;
	// }
}

Note: that the second else statement only has one line with it.

After that without the rest of the code it makes it difficult to test the function.

Andy
Topic archived. No new replies allowed.