summaryrefslogtreecommitdiffstats
path: root/C++/GuessingGame/GuessingGame.cpp
blob: c1e175472a92266f4d1d73dec8b833797037e93e (plain) (blame)
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
// Name: msglm
// Date: 
// Program Name:
// Description: 

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>

using namespace std;

// Named constants

int main() {

	//Variable declaration
	int tries;
	int correctNum;
	int userInput;
	bool invalidInput = 0;
	//Randomness
	srand(time(0));
	correctNum = rand() % 100;

	//Program title and description for the user
	cout << "Title: Guessing game" << endl << "Description: Makes the player choose a number between 1 and 100" << endl;

	// User input
	do {

		do {
			if (invalidInput) {
			//Clear the input if what was inputted was non-standard
			cin.clear();
			cin.ignore(100,'\n');
			invalidInput = 0;
			}
			
			//User instruction and input
			cout << "You have " << 10-tries << " number of tries left\n";
			cout << "Guess a number between 1 and 100 (inclusive): \n";
			cin >> userInput;
			
			//Make sure input is valid
			//Reason to declare a bool for this is so that
			//invalid input doesn't count as a "try"
			//Without this, either the player would have to input the first number twice
			//or the player would waste a try for a misclick
			if (!cin || userInput > 100 || userInput < 0) {
				invalidInput = 1;
			}



		} while (!cin || userInput > 100 || userInput < 0); //If input was bad, loop over to allow it to be checked, cleared, and ignored


		//Detection if how high or low the numbers are
		if (userInput > correctNum) {
			cout << "The correct number is lower than your input\n\n";
		} else if (userInput < correctNum) {
			cout << "The correct number is higher than your input\n\n";
		} else {
			cout << "You succeeded!\n";
			return 0; 
			//Exit program upon success
			//I prefer not to have my cli programs ask to loop
			//If I wish to play again, I will merely run the program again.
			//Also it was not in the instructions
		}
		tries++;
	} while (tries < 10);
	//Fail condition
	//Return code of 1 so that shell scripting can be aware if player failed the game
	//maybe someone would write a tournament script for this?
	//Unlikely, but possible.
	cout << "You ran out of tries, you failed!\n";
	cout << "The number was: " << correctNum << endl;
	return 1;
}

/*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */