Console Print out Checker board

Ch 8, Prog. excercise 8-1, practical c++ programming second edition

I am asked to print to console a 8 by 8 grid

+----+----+
| | |
| | |
| | |
+----+----+ like this

Here is the code that I used. Prints only the first line.

#include <iostream>

using std::cin;
using std::cout;

char withPlus[50]="+-----+-----+-----+-----+-----+-----+-----+-----+";
char wOutPlus[50]="| | | | | | | | |";
int lineCount=1;

int main()
{
while(lineCount < 34){

if (lineCount == (1||5||9||13||17||21||25||29||33))
cout << withPlus <<'\n';

else if (lineCount == (2||3||4||6||7||8||10||11||12||14||15||16||18||19||20||22||23||24||26||27||28||30||31||32))
cout << wOutPlus << '\n';


++lineCount;
continue;
}

return 0;
}




Some of the above formating has been butchered by the forum but hopefully the general idea is there.
Last edited on
Some of the above formating has been butchered by the forum but hopefully the general idea is there.

Edit your post and look for the format options to the right of the editbox. Highlight your code and click the one that says "code", it looks like this: <>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (lineCount == (1||5||9||13||17||21||25||29||33))
//it doesn't work that way

//try
if( lineCount % 4 == 1 )

//--------------------------------------------------
//What's happening is
(1||5||9|| ... ||33) //is evaluating to
(true||true||true|| ... ||true) //which of course is just plain
true //and then
lineCount == true //is evaluating either to
lineCount == 1 //or
lineCount != 0 //Whoops! 

1
2
3
4
5
6
else if (lineCount == (2||3||4||6||7||8||10||11||12||14||15||16||18||19||20||22||23||24||26||27||28||30||31||32))
//same thing here

//try
else if( lineCount % 4 != 1) //or better yet just plain
else

1
2
3
4
5
6
while( ... ) {
  ...
  continue;
  //You don't need the last line of your loop to be continue;
  // that's what loops do!
}

1
2
3
4
5
6
7
8
//Why are these three variables defined in global scope (i.e. outside the main method?)
char withPlus[50]="+-----+-----+-----+-----+-----+-----+-----+-----+";
char wOutPlus[50]="| | | | | | | | |";
int lineCount=1;

int main() {
  ...
}
Last edited on
Topic archived. No new replies allowed.