Error: lvalue required as increment operand

I'm receiving error: lvalue required as increment operand.

Error:

Project1.cpp:131:37: error: lvalue required as increment operand
for (unsigned i = 0; i < len; ++1)
^
Project1.cpp:138:14: error: ‘Mon’ was not declared in this scope
os << Mon[month(date) - 1];
^~~
Project1.cpp:144:34: error: lvalue required as increment operand
for (unsigned i = 0; i < len; ++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

  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;
  // }
}
 
++1
"plus plus one"

Does that look right to you? At a rough guess you mean "++i" - "plus plus eye"


As for the other error messages ... use curly braces { } to delimit your block structure. Your current indentation does NOT reflect it. C++ doesn't work like Python.
Last edited on
Hello thishas,

To go along with what lastchance has said.

For a for loop, if statement or while loop that one single line of code the {}s are not necessary. If any of these have two or more lines of code the {}s are necessary.

Properly formatted your first for loop would look like this:
1
2
3
for (unsigned i = 0; i < len; ++i)
	os << '0';
os << year(date) << endl;

Meaning that only line 2 goes with the for loop. If your intention is for both lines to be part of the for loop then it should look like this:
1
2
3
4
5
for (unsigned i = 0; i < len; i++)
{
	os << '0';
	os << year(date) << endl;
}

If you notice in each bit of code the third part of the for statement is different. Either way, pre or post increment, works out the same. The "i++" is most often seen. If you have not learned about this yet ""i++" is the short way of writing "i = i + 1;"

You may find this helpful http://www.cplusplus.com/doc/tutorial/control/#for although the whole page is worth looking at.

Hope that helps,

Andy
Andy / Lastchance ..

thank you for your input.
Registered users can post here. Sign in or register to post.