minesweeper game validation help

hi, im relatively new to c++ and i have created a minesweeper program. so far ive got quite far but im having problems with validation of input when the player inputs the coordinates (i basically dont know how to do it)if you could point out any other problems with solutions that would be great, thanks

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include<iomanip>
using namespace std;
bool gameover=false;

int turncount=0, flaggedmine=0, bomb, reveal, count=0, row, col, mine[8][8]={0,0,1,1,1,0,0,0,
																			 0,0,1,4,3,2,1,0,
																			 0,0,2,3,4,4,2,1,
																			 0,0,1,4,3,3,4,2,
																		 	 0,0,2,2,2,1,2,4,
																			 0,0,1,4,2,1,2,1,
																			 1,1,1,1,2,4,2,1,
																			 4,1,0,0,1,1,2,4};
		
char r, f, action, blank=240, option, block=178, mine2=157, vert=179, horiz=196, topleft=218, leftcorner=195, topmiddle=194, topright=191, middle=197, mright=180, hidden=178, 
	player[8][8];

void clear()
{
	for(row=0;row<8;row++)
		for(col=0;col<8;col++)
			player[row][col]=hidden;
}
void winorlose()
{
			
			cout<<"  0 1 2 3 4 5 6 7"<<endl;
			cout<<" "<<topleft<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topright<<endl;
		for (row=0; row<8; row++)
			{
				
				cout<<count;


				for (col=0; col<8; col++)
				{
					
					cout<<vert<<mine[row][col];

				}
				count++;
				cout<<vert;
				cout<<endl;
				cout<<" "<<leftcorner<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<mright;
				cout<<endl;
				
			}

}

void play()
{
	clear();
	while (gameover!=true || turncount>63)
			{
				turncount++;
			cout<<"  0 1 2 3 4 5 6 7"<<endl;
			cout<<" "<<topleft<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topmiddle<<horiz<<topright<<endl;
		for (row=0; row<8; row++)
			{
				
				cout<<count;


				for (col=0; col<8; col++)
				{
					
					cout<<vert<<player[row][col];

				}
				count++;
				cout<<vert;
				cout<<endl;
				cout<<" "<<leftcorner<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<middle<<horiz<<mright;
				cout<<endl;
				
			}
			count=0;
			cout<<flaggedmine<<endl;
			cout<<"Turn count ="<<turncount<<endl; 
			cout<<"Input the row of your selected coordinate: "<<endl; //player input
			cin>>row;
			cout<<"Input the column of your selected coordinate: "<<endl;
			cin>>col;
			cout<<"Would you like to flag (f) or reveal (r) this coordinate?: "<<endl;
			cin>>action;
			if(action=='r' || 'f')
			{
				{
					if(action=='r') 
					{
						if (mine[row][col]<4) 
						{
							player[row][col]=mine[row][col]+48;
						}
						else if (mine[row][col]==4) 
						{
							player[row][col]=mine[row][col];
							
							cout<<" YOU LOSE! "<<endl;
							winorlose();
							gameover=true;
						}
						} 
						else if(action=='f')
							{
								player[row][col]='F';

								
								
							}
						if(mine[row][col]>3)
								{
									flaggedmine++;
								}
						if(mine[row][col]<4)
								{ 
									flaggedmine--;
								}

						if(flaggedmine==10) // if all mines have been flagged correctly without any coordinates without mines being flagged, then game over becomes true and the player wins
							{
								cout<<"Congratulations, all mines have been found and you win!"<<endl;
								gameover=true;
								winorlose();
								clear();
								flaggedmine=0;
								turncount=0;
								count=0;
							}
				
				}
			}
			else 
			{
				cout<<"You have entered the wrong input, please enter r or f"<<endl;
			}
			
			
} 
}
void main()
{
	do
		{
		gameover=false;
		cout<<setw(5)<<" "<<"Minesweeper "<<endl<<endl;
		cout<<setw(5)<<" "<<"1. Beginner"<<endl;
		cout<<setw(5)<<" "<<"2. Intermediate"<<endl;
		cout<<setw(5)<<" "<<"3. Expert"<<endl;
		cout<<setw(5)<<" "<<"Q. Quit system"<<endl;
		cout<<setw(5)<<" "<<"Enter option choice"<<endl;
		cin>>option;
		switch(option)
		{	
		case '1':play();
			
			break;

		case '2':
			break;

		case '3':
			break;

		case 'Q':
			break;

			default: cout<<setw(5)<<" "<<"Incorrect option choice entered, please try again"<<endl<<endl;
		}
		}
	while (option!='Q' && option!='q');

	{
	system("pause");

	}

}	
Last edited on
You have a problem with the following if statement:
 
if(action=='r' || 'f')


C++ does not support an implied left hand side. You must fully specify both conditions.
 
if (action=='r' || action=='f')


To validate input use a do while loop as you did in main.
1
2
3
4
  do 
  {  cout<<"Input the row of your selected coordinate: ";
      cin>>row;
  } while (row < 0 || row >= 8);


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



thank you :D
Topic archived. No new replies allowed.