i would have posted this in tutorials but it's off topic so
In this tutorial we will make a game in the output using C++.
Requirements
 Basic understanding of C++ so you actually know what you are doing
 Repeat first step
How it will work
It is important that you have everything planned out.
Here is how one might plan this game:

The user will be prompted to input a number which will be how many tries they can get to guess the number. Number is between 310.

The minimum tries will be 3 and the maximum is 10. If it goes too low, or too high, it will be constrained in between 3 and 10.

The random number will be between 110. The user will get points for guessing correctly. They will lose points if they guess wrong.
Declaring some variables
Let's start with some variables, them being tries
, points
, randNum
, guessedCorrectly
, guessedTooLow
, and guessedTooHigh
.
The first three will be of type int
. The first will be how many tries they get. The second is how many points they have. The third will be the number to guess. The last three will be of type bool
and determine if they guessed correctly, too low, or too high respectively.
#include <iostream> // so we can use std namespace
using namespace std; // so we don't type std:: every time
int tries; // predeclare
int points = 0; // starting with 0 points
bool guessedCorrectly, guessedTooLow, guessedTooHigh; // predeclaring
int main() {
return 0;
}
Getting input
We will use cin
to get input. Here is a quick example on how to use it.
int num;
cout << "Input a number\n";
cin >> num;
cout << "Did you input " << num << "?\nI know you did!\n"
num
is predeclared and whatever is inputted, num
will contain the number.
>>
is the extraction operator. The num
variable follows where the extracted data is. There is a better explanation here on both cout
and cin
, and more.
Getting random numbers
There is a rand
function which you can use if you #include <cstdlib>
.
It generates an integer between 0 and "RAND_MAX"
A way to get a number in a specific range is modulating (or whatever the verb of modulus is) what is returned by rand
.
Here is an example:
int num = rand()%100;
cout << num;
The number is in the range 099. So you add +1 so it is in the range 1100.
int num = rand()%100 + 1;
cout << num;
Much better!
The fun stuff
Prompting user to pick a number between 310
With our knowledge of cin
we can prompt the user to choose number. I like having it tidy, so it will be in a function that can be called as much as we need to:
int prompt() {
int timesToPlay;
cout << "How many times do you want to try? (Number should be between 3 and 10)\n";
cin >> timesToPlay;
tries = clamp(timesToPlay, 3, 10);
for (int i = 1; i <= timesToPlay + 1; i++) {
play(randNum); // we'll get here don't worry
}
return timesToPlay;
}
timesToPlay
contains the number which is how many times to play the game. You will notice a clamp
function. It is there to constrain the number in between 310.
float clamp(float num, float min, float max) {
if (min > max) {
throw runtime_error("min must be smaller than max\n");
return 0;
}
if (num < min) {
return min;
}
if (num > max) {
return max;
}
return num;
}
Game logic
You may have noticed a play
function. This is where the game logic is handled.
void play(int num) {
}
num
is the random number they will have to guess.
Next, we will just get their input and check that input. But before we do that you want to make sure they have enough tries in the first place. If they do not, return out of the function. This is known as a guard.
void play(int num) {
if (tries < 1) {
cout << "Out of tries.\n";
return;
}
int guess;
cout << "Guess the number!\n";
cin >> guess;
}
After that, just check if the guess was too low, or too high, and set guessedTooLow
and guessedTooHigh
to true
for the respective conditions.
void play(int num) {
if (tries < 1) {
cout << "Out of tries.\n";
return;
}
int guess;
cout << "Guess the number!\n";
cin >> guess;
if (guess < num) {
// guessed too low
guessedCorrectly = false;
guessedTooLow = true;
guessedTooHigh = false;
}
else if (guess > num) {
// guessed too high
guessedCorrectly = false;
guessedTooHigh = true;
guessedTooLow = false;
}
}
If it's not too low or too high, it's a correct guess!
void play(int num) {
if (tries < 1) {
cout << "Out of tries.\n";
return;
}
int guess;
cout << "Guess the number!\n";
cin >> guess;
if (guess < num) {
// guessed too low
guessedCorrectly = false;
guessedTooLow = true;
guessedTooHigh = false;
}
else if (guess > num) {
// guessed too high
guessedCorrectly = false;
guessedTooHigh = true;
guessedTooLow = false;
}
else {
guessedCorrectly = true;
guessedTooLow = false;
guessedTooHigh = false;
}
tries; // don't forget to decrement tries!!!
}
When they did not guess correctly, you surely would want to decrement the user's points and tell them they guessed wrong.
Let's add that.
Whilst we are at that, let's also check if they did guess correctly. If so, increment their points and tell them they guessed correctly.
void play(int num) {
if (tries < 1) {
cout << "Out of tries.\n";
return;
}
int guess;
cout << "Guess the number!\n";
cin >> guess;
if (guess < num) {
// guessed too low
guessedCorrectly = false;
guessedTooLow = true;
guessedTooHigh = false;
}
else if (guess > num) {
// guessed too high
guessedCorrectly = false;
guessedTooHigh = true;
guessedTooLow = false;
}
else {
guessedCorrectly = true;
guessedTooLow = false;
guessedTooHigh = false;
}
tries; // don't forget to decrement tries!!!
if (!guessedCorrectly) {
points; // decrementing points
if (guessedTooLow) {
cout << "You guessed too low. 1 point. Points: " << points << "\n";
}
else if (guessedTooHigh) {
cout << "You guessed too high. 1 point. Points: " << points << '\n';
}
}
else { // guessed correctly
points++;
cout << "You guessed correctly! +1 point. Points: " << points << '\n';
}
}
And that is it!
The final product looks more or less like so:
#include <iostream>
#include <cstdlib> // lets us use rand()
using namespace std;
int tries; // predeclare
int points = 0; // starting with 0 points
bool guessedCorrectly, guessedTooLow, guessedTooHigh; // predeclaring
float clamp(float num, float min, float max) {
if (min > max) {
throw runtime_error("min must be smaller than max\n");
return 0;
}
if (num < min) {
return min;
}
if (num > max) {
return max;
}
return num;
}
void play(int num) {
if (tries < 1) {
cout << "Out of tries.\n";
return;
}
int guess;
cout << "Guess the number!\n";
cin >> guess;
if (guess < num) {
// guessed too low
guessedCorrectly = false;
guessedTooLow = true;
guessedTooHigh = false;
}
else if (guess > num) {
// guessed too high
guessedCorrectly = false;
guessedTooHigh = true;
guessedTooLow = false;
}
else {
guessedCorrectly = true;
guessedTooLow = false;
guessedTooHigh = false;
}
tries; // don't forget to decrement tries!!!
if (!guessedCorrectly) {
points; // decrementing points
if (guessedTooLow) {
cout << "You guessed too low. 1 point. Points: " << points << "\n";
}
else if (guessedTooHigh) {
cout << "You guessed too high. 1 point. Points: " << points << '\n';
}
}
else { // guessed correctly
points++;
cout << "You guessed correctly! +1 point. Points: " << points << '\n';
}
}
int prompt() {
int timesToPlay;
cout << "How many times do you want to try? (Number should be between 3 and 10)\n";
cin >> timesToPlay;
tries = clamp(timesToPlay, 3, 10);
for (int i = 1; i <= timesToPlay + 1; i++) {
play(randNum); // we'll get here don't worry
}
return timesToPlay;
}
int main() {
prompt();
return 0;
}
Credits
@TheAlphaStigma
He helped out in writing the code, he made the whole part where we use bool
s to check if guessed correctly, too low or too high, possible.