numbers guessing game

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 4 Exercise 4
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 4

#include "std_lib_facilities.h"

int main() {

	int max = 100;
	int min = 0;
	int check = 0;
	string answer = " ";


	while (check == 0) {
		cout << "Is your number smaller, larger or equal to " << (max + min) / 2 <<"? Enter s for smaller, l for larger or e for equal.\n";
		cin >> answer;
		if (answer == "s") {
			max = (max + min) / 2;
		}
		else if (answer == "l") {
			min = (max + min) / 2;
		}
		else if (answer == "e") {
			cout << "Your number is: " << (max + min) / 2 << "\n";
			check = 1;
		}
	}
	keep_window_open();
}
Output:
Is your number smaller, larger or equal to 50? Enter s for smaller, l for larger or e for equal.
s
Is your number smaller, larger or equal to 25? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 37? Enter s for smaller, l for larger or e for equal.
s
Is your number smaller, larger or equal to 31? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 34? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 35? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 36? Enter s for smaller, l for larger or e for equal.
e
Your number is: 36
Please enter a character to exit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.