The third task was to create a program that picks a random integer in the range of 1 to 100. After that , the game starts when the user has to guess which number does the program picked, with hints of ’too high’ or ’too low’.
The program continues to run until the user guesses the integer.
I added some extra steps, because I wanted to show more information to the users , like:
¿How many guesses they had to make to get the right answer?
Create a program that generates a random number within 1 and 100, leaving the user to guess which number it is.
The Process
The Code
#include <iostream> #include <ctime> #include <cstdlib> using namespace std;
int main() { srand(time(NULL)); //sets the random seed int actual= rand() % 100 +1, given, counter=0; //Were actual be equal to “rand() % 100, it would have a value between 0 to 99” cout<<“Guess the number, it has a value from 1 to 100″<<endl; cin>>given; count=count+1; do { if(given>actual) //if given number is bigger than the actual one, the user will try again… { cout<<“Too high, give a smaller number…”<<endl; cin>>given; count=count+1; } else if (given<actual) // if given number is smaller than the actual one, the user will try again… { cout<<“Too low, give a bigger number…”<<endl; cin>>given; count=count+1; } } (while given!=actual)//The loop will be done until given is equal to the actual number
cout<<“You got the right number!”<<endl<<“It took you “<<counter<<” times to get the right number”<<endl; return 0;
}
The topics
#Importing and using libraries
#Transversal topic: Ability to create C++ file and run from command line (terminal)
In this homework the user is going to guess the number that you have created, the number is created randomly. The program is going to give clues to the user by telling him if he is under or above the secret number. For that we are going to use a function that creates numbers randomly “rand()” from the library “cstdlib”. For giving the clues we just need conditional structure. And we are going to add a loop for the user to dicide if he would want to play again.
# include <iostream>
# include <cmath>
# include <cstdlib>
using namespace std;
int randomnumber (){
int random;
random = rand()%100+1;
return random;
}
void pistas (int numus, int random){
if (numus==random){
cout <<“¡ADIVINASTE EL NÚMERO, FELICIDADES!”<<endl;
}else{
if(numus<random){
cout<< “El número es menor”<<endl;
} else if (numus> random){
cout<< “El número es mayor”<<endl;
} else if(numus<0 && numus>100){
cout << “El número esta fuera del rango”<<endl;
}
}
}
int main (){
int num, secret, contador=0;
char op;
cout<<“El chiste de este juego es adivinar el número dentro del rango del 1 al 100″<<endl;
do{
secret=randomnumber();
contador = 0;
do{
if (contador==0) {
cout<<“Escribe un número”<<endl;
} else {
cout<<“Intenta de nuevo”<<endl;
}
cin>> num;
pistas(num,secret);
contador++;
} while (num!= secret);
cout<<“Adivinaste el número en “<<contador<<” intentos”<<endl;
cout<<“¿Quieres seguir jugando?(s/n)”;
cin>>op;
}while(op==’s’||op==’S’);
}
Hi there, it´s been a while since las time, but today I have some cool code to have fun with your friends or your family. It´s a little game of guessing a random number, in which it will give you a little hint about it, but in the end it is a guess.
This is my third program, created using Cygwin and Atom.
The task was to create a program that picks a random integer in the range of 1 to 100. After that , the game starts when the user has to guess which number does the program picked, with hints of ’too high’ or ’too low’.
The program continues to run until the user guesses the integer.
I added some extra steps, because I wanted to show more information to the users , like:
-How many guesses they had to make to get the right answer.
The resources that helped me were: -How to think like a computer scientist.
-cpluplus.com
-Programming hub, mobile app.
Here you will find my code and some captures:
#include <iostream>
#include <stdlib.h> // srand , rand
#include <time.h> // time
using namespace std;
int main(){
int secret , guess, attempts;
cout << “This program picks a random integer in the range of 1-100.” << endl;
cout << “***In order to win , you need to guess the number***” << endl;
do {
cout << “Write your guess:”<< endl;
cin >> guess;
attempts=attempts+1;
if (secret<guess){
cout << “The secret number is lower.” << endl;
}
else if (secret>guess){
cout << “The secret number is higher.” << endl;
}
} while (secret!=guess);
cout << “You won , congratulations!” << endl;
cout << “It took you ” << attempts << ” attempts.” << endl;
return 0;
}
This assigment was: “Write a program that picks a random integer in the range of 1 to 100. It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program. The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.”
Made it! In this program I used loops for the program to continue until the user guesses. For the random integer I create an operation supporting by a post on Martin Cruz blog, we add two libraries: #include<stdlib.h>#include<time.h>.
Then we create the operation: variable=inferior limit+rand()%(superior limit +1 -inferior limit) and the number is created randomly. Now we continue with the loop and ifs, to validate when the number is too high or too low. Here’s the code:
This is it, the third program. This program needs a loop, in case the user wants to keep playing. You basically you just create it with a “do” and a “while”. To generate the random number you have to include two libraries: “ctime” and “cstdlib”. I included some few modifications to the program Ken asked. If you need any help, just ask for it!
int main()
{
int guess, num, cont ;
string res;
cout<<endl;
cout<<“Try to guess the number I have in mind (between 1 and 100).”<<endl;
do {
srand(time(NULL));
num=1+rand()%(101);
cont=4;
cout<<“You have 5 oportunities to guess.”<<endl;
cout<<“Please insert your guess:”<<endl;
cin>>guess;
while(num!=guess && cont>0)
{
cont=cont-1;
if(guess>num)
{
cout<<“The number “<<guess<<” is too high, please try again.”<<endl;
cin>>guess;
}
else
{
cout<<“The number “<<guess<<” is too low, please try again.”<<endl;
cin>>guess;
}
}
if(guess!=num)
{
cout<<“Good luck next time my friend, the number was “<<num<<“.”<<endl;
}
else
{
cout<<“Congratulations! You guessed the number in just “<<5-cont<<” attempts.”;
}
cout<<“You want to try your luck again? Yes/No.”<<endl;
cin>>res;
}
while(res!=”no” && res!=”NO” && res!=”No”);
return 0;
}
I have to say that this task is a little more complicated than the previous exercises, because it was necessary to use a special function to generate numbers randomly. This program was thought as a game, where the user have to guess a random number. I decided to give the user only five chances to … Continue reading Homework 3: Pick a number→