Free Online compiler and debugger tool for C/C++ languages — Fireside Analytics

Fireside Analytics

Making Data Science Accessible to Everyone

Check out this online compiler and debugger tool that lets you run code in your browser for projects in Python and C++. The tool is available here: OnlineGDB

We created this Number Guessing Game using the OnlineGDB tool. To play, scroll to the bottom of this code block and click 'run'. Guess the right number!

GDB online Debugger | Code, Compile, Run, Debug online C, C++

// Created by Shingai Manjengwa, Twitter: @Tjido
// Copyright 2018 Fireside Analytics Inc. All rights reserved.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    int lowerRange = 1, upperRange = 100;
    int secretNumber;
    int userGuess;
    int iteration;

// Generate the secret random number
    srand(time(0));
    secretNumber = (rand() % 100) + 1;

cout << "I've thought of a number between 1 and 100! Try to guess it." << endl;
    for (int counter = 5; counter > 0; counter--) {
        cout << "Range: [" << lowerRange << ", " << upperRange << "] , Number of guesses left: " << counter << endl;
        cout << "Your guess: ";
        cin >> userGuess;

if (userGuess == secretNumber) {
            cout << "Congrats! You guessed the right number in " << 6 - counter << " guesses." << endl;
            cout << endl;
            return 1;
        }

if (userGuess < secretNumber) {
            if (counter == 1) {
                cout << "Out of guesses! My number is " << secretNumber << endl;
            } else {
                cout << "Wrong! My number is bigger." << endl;
                cout << endl;
            }
            if (userGuess > lowerRange) {
                lowerRange = userGuess + 1;
            }
        } else {
            if (userGuess > secretNumber) {
                if (counter == 1) {
                    cout << "Out of guesses! My number is " << secretNumber << endl;
                } else {
                    cout << "Wrong! My number is smaller." << endl;
                    cout << endl;
                }
                if (userGuess < upperRange) {
                    upperRange = userGuess - 1;
                }
            }
        }
    }
    return 0;
}

Run the Code

fork me on onlinegdb