6
6
int getRand (int min, int max)
7
7
{
8
8
static const double fraction = 1.0 / (static_cast <double >(RAND_MAX) + 1.0 );
9
-
9
+
10
10
return static_cast <int >(rand () * fraction * (max - min + 1 ) + min);
11
11
}
12
12
@@ -19,7 +19,7 @@ bool getPlay()
19
19
std::cout << " keep playing? [y/n]" << ' \n ' ;
20
20
std::cin >> play;
21
21
std::cin.ignore (32767 , ' \n ' );
22
-
22
+
23
23
if (play == ' y' )
24
24
return true ;
25
25
else if (play == ' n' )
@@ -36,25 +36,25 @@ int main()
36
36
do
37
37
{
38
38
std::cout << " I'm thinking of a number from 1 to 100" << ' \n ' ;
39
-
39
+
40
40
int target = getRand (1 , 2 );
41
41
int guess (0 );
42
-
42
+
43
43
// game loop
44
44
for (int attempt = 0 ; attempt <= 7 ; ++attempt)
45
45
{
46
46
std::cout << " guess #" << attempt << " : " ;
47
47
std::cin >> guess;
48
-
48
+
49
49
// error correction for non integer input
50
50
if (std::cin.fail ())
51
51
{
52
52
std::cin.clear ();
53
53
std::cin.ignore (32767 , ' \n ' );
54
54
std::cout << " INVALID INPUT" << ' \n ' ;
55
55
}
56
-
57
- // checks if the guesd is correct
56
+
57
+ // checks if the guess is correct
58
58
else if (guess < target)
59
59
std::cout << " too low" << ' \n ' ;
60
60
else if (guess > target)
@@ -64,18 +64,18 @@ int main()
64
64
std::cout << " YOU WIN!" << ' \n ' ;
65
65
break ;
66
66
}
67
-
67
+
68
68
// let's player know they lost after 7 attempts
69
69
else if (attempt == 7 )
70
70
std::cout << " You loose" << ' \n ' ;
71
71
}
72
-
72
+
73
73
play = getPlay ();
74
-
74
+
75
75
}
76
76
while (play);
77
-
77
+
78
78
std::cout << " Thanks for playing!" ;
79
-
79
+
80
80
return 0 ;
81
81
}
0 commit comments