does not loop to default

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.


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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 #include <iostream>

using namespace std;

int main()
{

	char col;
	unsigned short row;
	char again;
	unsigned int 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);
}


Hello Jmor,

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.

Hope that helps,

Andy
Andy you are a lifesaver I just didn't understand what was going on thank you so much!!!
Topic archived. No new replies allowed.