diff options
Diffstat (limited to 'C++/GuessingGame')
-rw-r--r-- | C++/GuessingGame/Guessing Game Program Instructions.docx | bin | 0 -> 19199 bytes | |||
-rw-r--r-- | C++/GuessingGame/GuessingGame.cpp | 89 |
2 files changed, 89 insertions, 0 deletions
diff --git a/C++/GuessingGame/Guessing Game Program Instructions.docx b/C++/GuessingGame/Guessing Game Program Instructions.docx Binary files differnew file mode 100644 index 0000000..798663c --- /dev/null +++ b/C++/GuessingGame/Guessing Game Program Instructions.docx diff --git a/C++/GuessingGame/GuessingGame.cpp b/C++/GuessingGame/GuessingGame.cpp new file mode 100644 index 0000000..c1e1754 --- /dev/null +++ b/C++/GuessingGame/GuessingGame.cpp @@ -0,0 +1,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/>. + */ + |