c++编程入门by游戏编程

C++ Game Programming[1]

  • 1 Guess Number!

From book 《Beginning C++ Through Game Programming, 4th EDITION》Michael Dawson

1 Guess Number!

Basic ideas: The program will generate a random number. And you can input your guess to the program, then the program will tell you too high or too low, then you can modify your guess according to the response.

Here is a simple c++ example:

#include 
using namespace std;

int main()
{
	// seed random number generator
	int secretNumber = rand() % 100 + 1;
	//cout << secretNumber;
	int tries = 0;
	int guess;
	cout << "Welcome to Guess My number!!\n";
	do
	{
		cout << "Enter a guess: ";
		cin >> guess;
		++tries;
		if (guess > secretNumber)
			cout << "Too high!\n";
		else if (guess < secretNumber)
			cout << "Too low!\n";
		else
			cout << "\n That's it! You got it in " << tries << " guesses!\n";

	} while (guess != secretNumber);
	return 0;
}

Here is the executing result:
c++编程入门by游戏编程_第1张图片

你可能感兴趣的:(game,programming)