//Code is an RNG game // Charles I Montana //10/9/21 #include <iostream> using namespace std; int main() { //define constants and variables const int maxValue = 999; const int minValue = 100; while (true){ //output game instructions cout << "*****************************************" << endl; cout << "* Guess a Number between 100 and 999! *" << endl; cout << "*****************************************" << endl; //generate a random number srand ( time(NULL)); int random_number = (rand() % (maxValue - minValue+1)) + minValue; //initiate only 10 tries and output the number of tries remaining int tries =10; int user_input =0; while(tries > 0) { //validate user input and read the guess from the user cin >> user_input; if(user_input < minValue || user_input > maxValue) { cout << "You need to guess within the range idiot.\n"; } //bad choice and take away tries per choice else if(random_number > user_input) { tries -=1; cout << " You guessed too low " << endl; cout << " Sorry, you now have " << tries << " tries left." << endl; } else if(random_number < user_input) { tries -=1; cout << " You guessed too high " << endl; cout << " Sorry, you now have " << tries << "tries left." << endl; } //good choice else { cout << "Congratualtions! You have guessed correctly! \n"; break; } } //ask the user if they would like to play again or not play again and validate the input string play_choice = ""; while(play_choice!="y" and play_choice!="n") { cout << "Would you like to play again? Type y for Yes and n for No.:"; cin >> play_choice; if(play_choice=="y") { break; } else if(play_choice=="n") { return 0; } } } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}