'"Guess the number" game with C++ [closed]
I have an issue with a "Guess the number" game, as the title suggests.
I need to write a program where the at the beginning of the execution, the program should ask the customer to enter a minimum and the maximum number and tries count (e.g the user will define with how many tries the number will be guessed by themselves), after the user enters a minimum, maximum and tries count, the program should generate a random number between the user-defined minimum and maximum and give the user the number of tries to guess the number, which the user defined already.
If the user does not guess the number, output "Game over", if he guesses the number "Congratulations" the program execution should be stopped.
I am stuck at the point where I do not know how to generate the pseudo-random number between the minimum and maximum number which the user will enter and how to give the X number of tries to the user as well.
So far, I am here:
//REQUIREMENTS:
/*In the beginning of the execution, ask the customer to enter min and max number and tries count,
after the user enters min, max, tries count,
the program should generate random number between min and max and to give the user X number of tries to guess the number
If the user does not guess the number, output "Game over", if he guesses the number "Congratulations" and exit code 0!*/
/*------------------------------------------------------------------------------------------------------------------
/*Algorithm:
* 1. Define the variables
* 2. Output to the customer to enter the variable values
* 3. Input with the variables
* 4. for loop (since we know how many tries/iterations will be done as the user will define that)
* 5. The user should guess the generated random number
* 6. Output message depending on if he won or not - "Game over" or "Congratulations"
------------------------------------------------------------------------------------------------------------------*/
#include <iostream>
#include <cstdlib> //this header contains the srand() and rand() methods/functions for random number generation
int main() {
//The minimum and maximum numbers which define the range and the tries counter variables
int minNumber;
int maxNumber;
int triesCounter;
int randomlyGeneratedNumber;
//IO operations
std::cout << "Please, enter minimum number: ";
std::cin >> minNumber;
std::cout << "Please, enter maximum number: ";
std::cin >> maxNumber;
std::cout << "Please, enter the number of tries you will guess the number with: ";
std:: cin >> triesCounter;
/* initialize random seed: */
srand (time(NULL));
/* generate secret number between "minNumber" and "maxNumber": */
randomlyGeneratedNumber = rand() % minNumber + maxNumber;
//Using ternary operator instead of if/else conditionals
//($var==$var1)? std::cout << "Congratulations!" : std::cout << "Game over!";
return 0;
}
/*I've read quite a lot over the web on the stuff, however, most of the information is related to guessing games where the range of numbers is pre-defined, which is not the case with me and I am not an expert, but a beginner. */
//Could you please help me?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
