Kdrumz Kdrumz - 1 year ago 72
C++ Question

if conditions in checking location

I'm solving this problem and I am stuck on the two other if comditionsI have. I'm not sure how to check from b to g or 2-7. I'm also confused on how to check if all 8 spots are available. I always get confused when iterating or checking code with if and for loops. Here is what I have so far:

Code so far:

#include <iostream>
#include <string>

using namespace std;

int main(){

string c;

cin >> c;

if(c == "a8" || c == "h8" || c == "a1" || c == h1){

cout << "3 moves only available\n" << endl;
// confused on this case
if(c == c[0]( ){

cout << "5 moves only available\n" << endl;

// and this case
if(c ==){

cout << "all 8 moves are available\n" << endl;


Answer Source

First of all, h1 should be in inverted commas as it is a string literal, should look like this instead "h1".

Second, the case when there are only 5 moves are along the side of the board (but not corners). Thus you only need check if the file (column) is 'a' or 'h' or if the rank (row) is '1' or '8'. Use else if to shortened to code so that you don't output twice if it is a corner.

So your second case can look like this:

else if (c[0] == 'a' || c[0] == 'h' || c[1] == '1' || c[1] == '8){
    cout << "5 moves only available\n" << endl;

The last case is just:

    cout << "all 8 moves only available\n" << endl;

as all other cases have been dealt with

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download