From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- .../Guessing Game Program Instructions.docx | Bin 0 -> 19199 bytes C++/GuessingGame/GuessingGame.cpp | 89 +++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 C++/GuessingGame/Guessing Game Program Instructions.docx create mode 100644 C++/GuessingGame/GuessingGame.cpp (limited to 'C++/GuessingGame') diff --git a/C++/GuessingGame/Guessing Game Program Instructions.docx b/C++/GuessingGame/Guessing Game Program Instructions.docx new file mode 100644 index 0000000..798663c Binary files /dev/null and b/C++/GuessingGame/Guessing Game Program Instructions.docx differ 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 +#include +#include +#include +#include + +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 . + */ + -- cgit v1.2.3