I got asked by my teacher to do a chessboard, this is what it looks like when I build it , my problem is that my teacher wants it horizontally like A8, B8, C8, and when I build it it's A8, A7, A6, so I kinda need to swap it but I don't know-how.
#include <iostream>
using namespace std;
int main()
{
for(char i = 65; i < 73; i )
{
cout << "\n";
cout << i << "8" << " ";
cout << i << "7" << " ";
cout << i << "6" << " ";
cout << i << "5" << " ";
cout << i << "4" << " ";
cout << i << "3" << " ";
cout << i << "2" << " ";
cout << i << "1" << " ";
cout << "\n";
}
}
CodePudding user response:
You have to print letters from A to H on each line. Also, printing numbers is much easier with a loop too.
for(int i = 8; i >= 1; i--)
{
for(char c = 'A'; c <= 'H'; c )
{
std::cout << c << i << " ";
}
std::cout << '\n';
}
CodePudding user response:
You could consider using a nested loop, where the outer loop controls the number of horizontal rows and the inner loop controls the printing of character in each row. Something like the following:
#include <iostream>
using namespace std;
int main()
{
for(int i = 8; i >= 1; i--)
{
for(char j = 65; j < 73; j )
{
cout << j << i << " ";
}
cout << "\n";
}
}
This way, the outer loop controls the row number, which starts from 8 and goes all the way to 1. The inner loop, iterates 8 times (A - H) for 1 iteration of the outer loop and prints out one row. The same is repeated for all 8 rows.