Skip to content

Instantly share code, notes, and snippets.

@kgleeson
Created November 16, 2012 23:17
Show Gist options
  • Select an option

  • Save kgleeson/4091811 to your computer and use it in GitHub Desktop.

Select an option

Save kgleeson/4091811 to your computer and use it in GitHub Desktop.
Human guess
#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