In: Computer Science
Hi, I want to programm a c++ gamr without the using of any array. only by using FUNCTIONS, WHILE LOOPS, FOR LOOPS, IF ELSE, SRADN() and RAND(), BREAK STATEMENT, CIN and COUT. Please help me out.
//include headers
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//function to generate a random number between given range
[a,b]
int rand_int(int a,int b){
//using rand()
int num = (rand()%(b - a + 1)) + a;
return num;
}
//driver function
int main(){
//welcome message
cout<<"Welcome to the game of random numbers\n\n";
//seeding to current time so as to generate a different random
number every time
srand(time(NULL));
//Play till player quits the game
while(true){
//ask user to enter a range or to quit
cout<<"Please input a range i.e, [a,b] such that a<=b (or
enter both a and b as -1 to quit)\n";
//input range [a, b]
cout<<"Enter a: ";
int a;
cin>>a;
cout<<"Enter b: ";
int b;
cin>>b;
//if user wants to quit then break from loop
if(a==-1 && b==-1){
cout<<"Quitting game!\n";
break;
}
//if range is valid
if(a<=b){
//generate a random number between [a, b]
int num=rand_int(a,b);
//ask player to guess the number between range [a, b]
cout<<"Now guess a number between your provided range(if you
guessed the number generated by computer randomly then you win
otherwise you lose the game): ";
int input;
//input number form player
cin>>input;
//if guessed number equals random number then player wins.
if(input==num){
cout<<"Yay! you won!\n";
}
//otherwise loses.
else{
cout<<"Oops! You lose!\n";
}
}
//if range invalid then print invalid message and continue
else{
cout<<"Invalid range entered!\n";
}
}
return 0;
}
Welcome to the game of random numbers
Please input a range i.e, [a,b] such that a<=b (or enter both
a and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the
number generated by computer randomly then you win otherwise you
lose the game): 2
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a
and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the
number generated by computer randomly then you win otherwise you
lose the game): 5
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a
and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the
number generated by computer randomly then you win otherwise you
lose the game): 2
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a
and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the
number generated by computer randomly then you win otherwise you
lose the game): 3
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a
and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the
number generated by computer randomly then you win otherwise you
lose the game): 2
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a
and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the
number generated by computer randomly then you win otherwise you
lose the game): 2
Yay! you won!
Please input a range i.e, [a,b] such that a<=b (or enter both a
and b as -1 to quit)
Enter a: -1
Enter b: -1
Quitting game!
CODE
INPUT/OUTPUT
So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.