Hint please!!

I can't figure out what am I doing wrong with this program in which just by typing row and column user gets the color of chess block.

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int> (time(0)));
char answer;
int row;
char column;
cout << "Would you like to know a square on the chess board on the basis of row and column entered (y/n) ? " << endl;
cin >> answer;
cout << "Enter a row number and column letter of a square on a chess board" << endl;
cin >> row;
cin >> column;
while (toupper(answer) == 'Y')
{

int count = 0;
int num = 8;

if (toupper(column) == 'a', 'c', 'e', 'g')
{
if (row % 2 == 0)
{
cout << "Row, Col:" << row << " ," << column << " is a black space" << endl;
}
else
{
cout << "Row, Col:" << row << " ," << column << " is a white space" << endl;
}

}


else
{
if (row % 2 != 0)
{
cout << "Row, Col:" << row << " ," << column << " is a black space" << endl;
}


else
{
cout << "Row, Col:" << row << "," << column << " is a white space" << endl;

cout << "The number of valid coordinates entered was " << count << endl;


}

if (row < num || toupper(column) > 'h')
{
cout << row << column << " is not a valid coordinate!" << endl;
cout << " Row numbers are from 1 - 8 and column letters are from a - h." << endl;

}

}

}








return 0;

}
That is really hard to eyeball when you don't put your code in [code] blocks.
What is this line?
if (toupper(column) == 'a', 'c', 'e', 'g')
well it says that if column equals the letter ('a'|| 'c'|| 'e'|| 'g') then it processes further.
I am a beginner so sorry for doing it way wrong.
if (toupper(column) == 'a', 'c', 'e', 'g')

Should be :
if (toupper(column) == 'a' || toupper(column) == 'c' || toupper(column) == 'e' || toupper(column) == 'g')
Oh Thank you!! I don't know why i missed it but anyways when the code ends and when I run it, it just gets messed up... I have this code and i still have to add that if row exceeds 8 and column exceeds 'h' then there is an error message that it is an invalid input but the more i try the more messy it gets.

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int> (time(0)));
char answer;
int row;
char column;
cout << "Would you like to know a square on the chess board on the basis of row and column entered (y/n) ? " << endl;
cin >> answer;
cout << "Enter a row number and column letter of a square on a chess board" << endl;
cin >> row;
cin >> column;
while (toupper(answer) == 'Y')
{

int count = 1;
int num = 8;

if ( (toupper(column) == 'a' || toupper(column) == 'c' || toupper(column) == 'e' || toupper(column) == 'g'))
{
if (row % 2 == 0)
{
cout << "Row, Col:" << row << " ," << column << " is a black space" << endl;
}
else
{
cout << "Row, Col:" << row << " ," << column << " is a white space" << endl;
}

}


else
{
if (row % 2 != 0)
{
cout << "Row, Col:" << row << " ," << column << " is a black space" << endl;
}


else
{
cout << "Row, Col:" << row << "," << column << " is a white space" << endl;

cout << "The number of valid coordinates entered was " << count << endl;

}

}

}

return 0;

}
@Taha001

Sorry, but this..
if ( (toupper(column) == 'a' || toupper(column) == 'c' || toupper(column) == 'e' || toupper(column) == 'g')) is INCORRECT.

They should all be CAPITAL letters, not lower case since you are checking if they are capitals.

Or, change it to check for lower case, by..
if ( (tolower(column) == 'a' || tolower(column) == 'c' || tolower(column) == 'e' || tolower(column) == 'g'))
Thank you.... but when I add that if row exceeds 8 and column exceeds 'h' then there is an error message that it is an invalid input but the more i try the more messy it gets. The answer gets all messed up and doesn't show the right answer!!
@Taha001

You should check for correct inputs where they are entered.
1
2
3
4
5
6
do
{
cout << "Enter a row number" << endl;
cin>>  row;

}while (row<1 || row > 8);


1
2
3
4
5
6
do
{
cout << "Enter the column letter" << endl;
cin>>  column;

}while (tolower(column)<'a' || tolower(column) > 'h');
Now I have got this code and it is not right and i don't know what is wrong!!!.

include <iostream>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int> (time(0)));
char answer;
int row;
char column;
cout << "Would you like to know a square on the chess board on the basis of row and column entered (y/n) ? " << endl;
cin >> answer;
cout << "Enter a row number and column letter of a square on a chess board" << endl;
cin >> row;
cin >> column;
while (toupper(answer) == 'Y')
{

int count = 1;
int num = 8;

while (row<1 || row > 8 && tolower(column)<'a' || tolower(column) > 'h');
{
cout << row << ", " << column << "is not a valid coordinate! " << endl;
cout << "Row numbers are from 1 - 8 and column letters are from a - h." << endl;
}
if ((toupper(column) == 'A' || toupper(column) == 'C' || toupper(column) == 'E' || toupper(column) == 'G'))
{
if (row % 2 == 0)
{
cout << "Row, Col:" << row << " ," << column << " is a black space" << endl;
}
else
{
cout << "Row, Col:" << row << " ," << column << " is a white space" << endl;
}

}


else
{
if (row % 2 != 0)
{
cout << "Row, Col:" << row << " ," << column << " is a black space" << endl;
}


else
{
cout << "Row, Col:" << row << "," << column << " is a white space" << endl;


}


}
cout << "The number of valid coordinates entered was " << count << endl;
cout << "Would you like to check another square ? (Y / N)" << endl;
cin >> answer;

}

return 0;

}
@Taha001

One error is you're not getting a new row and column after requesting it the first time.
Here's your code showing in the correct order..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
//#include <ctime>

using namespace std;

int main()
{
	//srand(static_cast<unsigned int> (time(0)));// Why. Not usingg random numbers
	char answer = 'Y'; // Initialize answer
	int row;
	char column;
	int count = 1;
	int num = 8;
	do
	{
	cout << "Would you like to know a square on the chess board"<<endl<<"on the basis of row and column entered (y/n) ? " << endl;
	cin >> answer;
	answer = toupper(answer);
	if (answer != 'Y' && answer != 'N') // Get only a "Y' or an 'N'
	  {
		cout << "I don't understand. Please enter only a 'Y' or an 'N'" << endl << endl;
	  }
	} while (answer != 'Y' && answer != 'N');
	if (answer == 'Y')
	{
		do
		{
			cout << "Enter a row number ( 1 - 8 )" << endl;
			cin >> row;
		} while (row < 1 || row > 8);
		do
		{
			cout << "Enter the column letter ( A - H )" << endl;
			cin >> column;
		} while (column < 'A' || column > 'H');

		if (column == 'A' || column == 'C' || column == 'E' || column == 'G')
		{
			if (row % 2 == 0)
			{
				cout << "Row: " << row << " , col: '" << column << "' is a black space." << endl;
			}
			else
			{
				cout << "Row: " << row << " , col: '" << column << "' is a white space." << endl;
			}
		}
		else
		{
			if (row % 2 != 0)
			{
				cout << "Row: " << row << " , col: '" << column << "' is a black space" << endl;
			}
			else
			{
				cout << "Row: " << row << " , col: '" << column << "' is a white space" << endl;
			}
		}
		cout << "The number of valid coordinates entered was " << count << endl;
		count++;
	}
	} while (toupper(answer) == 'Y');

	return 0;

}

Last edited on
Topic archived. No new replies allowed.