-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA16customcheckerboard.cpp
64 lines (54 loc) · 1.36 KB
/
A16customcheckerboard.cpp
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
// Custom Checkerboard
// Objectives: Pass by value and reference,
// nested repetition,
// using global constants
// By Emily Dayanghirang
#include <iostream>
using namespace std;
const int MAX_WIDTH = 40;
const double MAX_HEIGHT = MAX_WIDTH/2;
void checkerboard(int);
void getWidth(int &);
int main()
{
int width;
getWidth(width);
checkerboard(width);
}
void checkerboard(int width)
{
for (int i = 0; i < MAX_HEIGHT; i++)
{
for(int j = 0; j < width; j++)
{
if((i%2==0 && j%2==0)||(i%2!=0 && j%2!=0))
cout<<".";
else
cout<<"*";
}
cout << endl;
}
}
void getWidth(int &width)
{
int userInput;
do
{
cout << "\nEnter an integer in the range of 1 to "
<< MAX_WIDTH << ": ";
cin >> userInput;
width = userInput;
// Input validation for int
if (cin.fail())
{
cout << "\nERROR: Please input an integer.\n";
cin.clear();
cin.ignore(1000, '\n');
}
else if((userInput < 1 || userInput > MAX_WIDTH) && cin)
{
cout << "\nERROR: Please input an integer in the "
<< "range of 1 to " << MAX_WIDTH << ".\n";
}
}while(userInput < 1 || userInput > MAX_WIDTH || cin.fail());
}