# WSQ03 – Pick a number.

--Originally published at Blogging through my thoughts

Good morning!

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.

¿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.

-cplusplus.com

-cppreference.com

-Programming hub, mobile app.

Until next time!

# WSQ03 – RANDOM

--Originally published at The Talking Chalk

Create a program that generates a random number within 1 and 100, leaving the user to guess which number it is.

## 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)

# Homework 3 Pick a number

--Originally published at Solving problems with programming

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.

Here you can acces to the code

# 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){
}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 (){
char op;
cout<<“El chiste de este juego es adivinar el número dentro del rango del 1 al 100″<<endl;
do{
secret=randomnumber();
do{
cout<<“Escribe un número”<<endl;
} else {
cout<<“Intenta de nuevo”<<endl;
}
cin>> num;
pistas(num,secret);
} while (num!= secret);
cout<<“¿Quieres seguir jugando?(s/n)”;
cin>>op;
}while(op==’s’||op==’S’);
}

# I’m on fire!

--Originally published at TC1017

Even though I like to program, I should really organize more when to program. I wrote the title because my fingers are hurt, thanks a lot life.

The program is about guessing a number. Sounds fun, right?

#BAPL

# WSQ03

--Originally published at TC1017 Solving Problems with Programming

Random number it is today!

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.

Have fun with it!

# WSQ 03 – Pick a number.

--Originally published at |Blogging through my thoughts|

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.

-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;

srand (time(NULL));               // Initialize random seed
secret = rand() % 100 + 1;

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;
}

Until next time.

°Luis Felipe Garate Moreno.

# WSQ03-Pick a Number

--Originally published at Programming course

In this program we had to make a random number and the user needed to guess it. If the number was to high or low, the program will tell.

First I made the random number called nu1.

Then I asked the user to guess and using if, I told the user if the number was higher or lower than the random one and made them choose again.

When they guessed the correct number I asked if they wanted to play again and that was it

# Pick a number! #WSQ03

--Originally published at It&#039;s me

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:

And the code running:

Regards!

# WSQ03

--Originally published at OlafGC / Class #TC1017

## Pick a number!

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!

## Code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

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;
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;
}

# Homework 3: Pick a number

--Originally published at Let&#039;s CODE

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