I have a bit of an issue I cant figure out, my code seems fine until I try to get it to loop to default my code will not loop there any thoughts on what I am doing wrong would be helpful.
#include <iostream>
usingnamespace std;
int main()
{
char col;
unsignedshort row;
char again;
unsignedint count = 1;
cout << " Enter a row rowber and column letter of a square on a chess board ";
cin >> row;
cin >> col;
if (row >= 8)
{
cout << row << "," << col << " is not a valid coordinate!." << endl;
cout << "Column rowbers are from 1 - 8 and row letters are from a - h." << endl;
cout << " Enter a row number and column letter of a square on a chess board ";
cin >> row;
cin >> col;
}
while (row <= 8)
{ //whilestart
switch (tolower(col))
{//switchstarts
case'a':
case'c':
case'e':
case'g':
if (row <= 8)
{
if (row % 2 == 0)
{
cout << "Row, Col: " << row << "," << col << " is a White space" << endl;
}
else
{
cout << "Row, Col: " << row << "," << col << " is a Black space" << endl;
}
}
break; // this break is for aceg
case'b':
case'd':
case'f':
case'h':
if (row <= 8)
{
if (row % 2 == 0)
{
cout << "Row, Col: " << row << "," << col << " is a Black space" << endl;
}
else
{
cout << "Row, Col: " << row << "," << col << " is a White space" << endl;
}
}
break; //this break is for bdfh
default:
cout << row << "," << col << " is not a valid coordinate!." << endl;
cout << "Column rowbers are from 1 - 8 and row letters are from a - h." << endl;
cout << " Enter a row number and column letter of a square on a chess board ";
cin >> row;
cin >> col;
count++;
break;
} //end of switch
cout << " Would you like to check another square? (Y/N):";
cin >> again;
if (again == 'y')
{
cout << " Enter a row number and column letter of a square on a chess board ";
cin >> row;
cin >> col;
count++;
}
else
{
cout << "The rnumber of valid coordinates entered was: " << count << endl;
break;
}
break;
} // while end
return (0);
}
The default of the switch can only be reached when col is greater than h. In the default you ask for row and col just to break out of switch just to ask again. If you are going to ask for row and col in default than consider using goto to go to the beginning of switch instead of the break. Or consider deleting lines 73 - 75 because they are redundant and line 75 is the wrong place to add to count.
Delete line 94. That break will only let the while loop work once.