guessing game c++ code example

Example: guessing game c++

#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;


int main() {

    long int randnum,guess,lowerlimit,higherlimit = 0 ;
    srand(time(NULL));

    do
    {
    cout<<"enter your lower limit number: ";
    cin>> lowerlimit ;
    cout<<"enter your higher limit number:";
    cin>>higherlimit;
    cout<<"enter your guessing number between: " <<"  "<< lowerlimit << " and "<<""<<higherlimit<<" : ";
    cin>>guess;
    randnum = rand()%(higherlimit-lowerlimit+1) + lowerlimit ;  
    // using the conditional operator instead of if / else  
    if(guess != randnum){
        cout <<((guess > randnum)? "your guess is large  " : "your guess is too small " ) << "and it is " << guess << " and computer guess is " << randnum << endl;

    }else
        cout << " GREAT, YOU WON !!!"<<", your guess is " << guess << " and computer guess is " << randnum << endl;
        
    }while(guess!=randnum);
    
    
    return 0 ; 

}

Tags:

Cpp Example