Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- #include <string>
- using namespace std;
- void game();
- int main() {
- string menu;
- cout << "Welcome to 'Guess a number'" << endl;
- cout << "Write 'Start' to start the game!" << endl;
- cin >> menu;
- if(menu == "start" || menu == "Start") {
- game();
- } else {
- return 0;
- }
- }
- void game() {
- srand(time(NULL));
- int high;
- int low;
- int guess;
- int number;
- bool gameLoop = true;
- cout << "Write down the highest number you want to guess: ";
- cin >> high;
- cout << endl;
- cout << "Write down the lowest number you want to guess: ";
- cin >> low;
- cout << endl;
- guess = rand() % high + low;
- while(gameLoop) {
- cout << "Guess a number: ";
- cin >> number;
- if(number > guess) {
- cout << endl << "<" << endl;
- } else if(number< guess) {
- cout << endl << ">" << endl;
- } else if(number == guess) {
- gameLoop = false;
- cout << "You won!" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment