Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to break up a statement into multiple lines?

Tags:

c++

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.

like image 464
Nick Serdaru Avatar asked Jan 20 '26 17:01

Nick Serdaru


1 Answers

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.

like image 161
Barmar Avatar answered Jan 22 '26 07:01

Barmar