Created
July 28, 2018 20:40
-
-
Save Elsayegh/bef78f50b1bdcc421f23cf11f60af572 to your computer and use it in GitHub Desktop.
C++ Guess The Number Game - Using Loops and If Statements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*Write a program that generates a random number and asks the user to guess what the number is. | |
| If the user's guess is higher than the random number, the program should display "Too high, try again". | |
| If it is lower, it should display "Too low, try again". | |
| The program should use a loop that repeats until the user correctly guesses the random number. | |
| */ | |
| #include <iostream> | |
| #include <conio.h> | |
| #include <ctime> | |
| #include <iomanip> | |
| #include <cstdlib> | |
| using namespace std; | |
| int main() { | |
| int randomNumber; | |
| int userNumber; | |
| bool win = false; | |
| int numOfAttempts = 0; | |
| unsigned seed = time(0); | |
| srand(seed); | |
| randomNumber = 1 + rand() % 10; | |
| cout << "i am thinking of a number can you guess it? " << endl; | |
| cin >> userNumber; | |
| while (!win) { | |
| if (userNumber < randomNumber) { | |
| cout << "lower number , try again "; | |
| cin >> userNumber; | |
| numOfAttempts++; | |
| } | |
| else if (userNumber > randomNumber) { | |
| cout << "higher number, try again "; | |
| cin >> userNumber; | |
| numOfAttempts++; | |
| } | |
| else { | |
| cout << "Congratulations, you guessed the correct number in " << numOfAttempts << " Times" << endl; | |
| win = true; | |
| } | |
| } | |
| _getch(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment