Random Number

--Originally published at PZ

This was much more challenging work thant the last ones becuase I used a lot of variables and libraries that I didnt know like time, srand and rand. So I started to research in the internet and I found this page: http://www.cplusplus.com/reference/cstdlib/rand/, which helps me a lote because I used to understand the variables that I used, then I added iostream and std so I replaced all the scan and print to cin and cout, so this become much easier for me. Finally I ran the game and I saw that this actually works and its very funny to play with it.

#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main ()
{
int a, b;

 

srand (time(NULL));
a = rand() % 100 + 1;

do {
cout<< “Guess the number (1 to 100): “<<endl;
cin>>b;
if (a<b) cout<< “The secret number is lower”<<endl;
else if (a>b) cout<< “The secret number is higher”<<endl;
} while (a!=b);

cout<<“Congratulations”<<endl;
return 0;
}

 

Links

Cplusplus, function rand: http://www.cplusplus.com/reference/cstdlib/rand/