Created
November 16, 2012 23:17
-
-
Save kgleeson/4091811 to your computer and use it in GitHub Desktop.
Human guess
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
| #include <iostream> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| using namespace std; | |
| int main(){ | |
| int guess; | |
| int guess_attempts = 0 ; | |
| srand (time(NULL)); | |
| int answer = rand() % 100 + 1; | |
| cout << "Guess the number!\nYour guess: "; | |
| do{ | |
| guess_attempts++; | |
| if(!(cin >> guess)){ | |
| cout << "You did not enter a number, try again." << endl; | |
| cin.clear(); | |
| cin.ignore(10000, '\n'); | |
| } | |
| cout << "Your guess is "; | |
| if(guess < answer){ | |
| cout << "too low" << endl;} | |
| if(guess > answer){ | |
| cout << "too high" << endl;} | |
| cout << "Your guess: "; | |
| }while(guess != answer); | |
| cout << "correct!\n You took " << guess_attempts <<" guesses." << endl;} | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment