2

I'm currently in an extremely basic C++ class and we are writing a program to check if an input is a string or has digits within it. I currently have my code and everything seems to be in order however, when I attempt to test it I get the error:

Line should be less than or equal to 80 characters long.

The error location reveals this as the problem:

if ((checknum == 1 && checkVector[0] == 1)|| checknum == 0 || checknum == strlength) {

Is there any way to break this up into multiple lines to avoid this error? It will not let me run it without all of the white spaces.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • FYI, `and` and `or` [are also C++ keywords](http://stackoverflow.com/questions/2376448/the-written-versions-of-the-logical-operators) and much easier to read. You are also much less likely to accidentally throw in a bitwise `|` or `&`. – Aaron D. Marasco Feb 14 '17 at 23:07

3 Answers3

5

You can put line breaks anywhere that whitespace is allowed:

if ((checknum == 1 && checkVector[0] == 1)||
     checknum == 0 || 
     checknum == strlength) {

Or you could set variables:

int check1 = checknum == 1 && checkVector[0] == 1;
int check2 = checknum == 0;
int check3 = checknum == strlength;
if (check1 || check2 || check3) {

This isn't exactly equivalent, because || performs short-circuiting, and doesn't bother evaluating later operands if an earlier operand is true. But since there are no side effects or expensive calculations in your expressions, the difference is negligible.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

C++ is whitespace-insensitive, and newlines are whitespace. So you can break the line at any token boundary you like.

For example:

if
(
   (checknum == 1 && checkVector[
0] == 1)|| checknum

                  ==      0 ||
     checknum == strlength
      ) {
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

Sure, you can add as much whitespace as you want. For example, this is allowed (but perhaps not a good idea):

if
(
(
checknum
==
 1
 && 
checkVector
[
0
]
==
 1
)
||
 checknum
 == 
0
 ||
 checknum
 == 
strlength
)
 {
Bo Persson
  • 90,663
  • 31
  • 146
  • 203