[ create a new paste ] login | about

Link: http://codepad.org/m4ih1Wjn    [ raw code | fork ]

C++, pasted on Feb 10:
#include <iostream>
#include <cctype> 
using namespace std;

int main(int argc, char** argv) {
	unsigned int minimum = 1, maximum = 100, guesses = 0;
	while(true) {
		if(minimum > maximum) {
			cout << "WAT D:<\n";
			return 0;
		}
		unsigned int guess = (minimum + maximum) >> 1;
		cout << "Is your number " << guess
		     << "? Yes(y), too high(h), or too low(l): ";
		string ans;			
		cin >> ans;
		switch(tolower(ans[0])) {
			case 'y':
				cout << "Machines prevail in " << ++guesses << " move";
				cout << ((guesses == 1) ? "" : "s") << ".\n";
				return 0;
			case 'h':
				maximum = guess - 1;
				guesses += 1;
				break;
			case 'l':
				guesses += 1;
				minimum = guess + 1;
		}
	}
}


Create a new paste based on this one


Comments: