Third assignment – Pick a number

--Originally published at The Clueless Programmer

Okay so this assignment really tested me out, specially because I didn´t know anything about generating a random number or how to do it, but by reading the book (and asking a lot of questions) I figures it out. Let´s go.

The tasks asks us to make a program that thinks of a random number and then starts asking us to guess the number, telling us if we are lower or higher, once we guess it, it gets us out.

imagen1

#include <iostream>
#include <stdlib.h>//Library for the rand command
using namespace std;
int main()
{
int n1, n2;
char cond=’n’;
srand(time(NULL));//This makes the number always random
n1=rand()%101;//I use the 101 because it takes every number until the number you write
do
{
cout<<“Try to guess the number I´m thinking of”<<endl;
cin>>n2;
if (n2>n1)
{
cout<<“Your number is higher than the one I´m thinking of”<<endl;
}
else if(n2<n1)
{
cout<<“Your number is lower than the one I´m thinking of”<<endl;
}
else
{
cout<<“Congrats! You guessed it”<<endl;
cond=’s’;
}
} while(cond==’n’);
}

So okay you are confused, you see a lot of tricky stuff right? But don´t worry it will make sense, eventually.

First what you want to do is include the library that has the command for the random number generator. Then after you name your variables, you write that srand stuff, which basically what it does is change the random command so that everytime you enter the number is different. Then you write the rand command, I write 101 instead of 100 because it takes the number before the oe you write, so 0-100. Then we enter the do-while, which basically means it ill do something at first and then continue to do it while the condition isn´t fulfilled. I do the ifs and else in which I close out

Continue reading "Third assignment – Pick a number"