hello_c++.cpp

July 21, 2019
Hello World C++

C++

For a while I’ve been meaning to dive into a lower language such as C++, but never found the time to do so. Now I have an excuse as my new employment involves working with C++. To start, like with anything I made a simple hello world program. This looks like the following…

#include <iostream>

int main() {
    std::cout << "Hello world from C++!\n";
    return 0;
}

Now to compile the program via g++ main.cpp -o main and run it via ./main.

The output is Hello world from C++!.

The next program I made was a number guessing game, similar to the one I made when first learning Python. This programs looks like the following…

#include <iostream>
#include <time.h>

void play();
int32_t getGuess();
bool again();

int main() {
    bool doAgain;

    while(doAgain == 1){
        system("clear");
        play();
        doAgain = again();
    };

    return 0;
}

void play() {
    srand(time(NULL)); // Setting the seed
    int32_t rngNum = rand() % 10; // Generate random number between 0-10
    int32_t guesses = 0;
    int32_t guess = -1;

     while(guess != rngNum) {
        guess = getGuess();

        if (guess != rngNum) {
            if (guess > rngNum) {
                std::cout << "Guess was too high!\n";
            } else {
                std::cout << "Guess was too low!\n";
            }
            guesses++;
        } else {
            guesses++;
            std::cout << "You win! It took you " << guesses << " guesses.\n";
        }
    };
}

int32_t getGuess() {
    int32_t guess = -1;

    std::cout << "Guess: ";
    std::cin >> guess;

    while(std::cin.fail()) {
        std::cout << "Please enter a number ...\n";
        std::cin.clear(); // Unset failbit; see std::cin.fail()

        // https://en.cppreference.com/w/cpp/io/basic_istream/ignore
        std::cin.ignore(256,'\n');
    }

    return guess;
}

bool again() {
    char ans = 'n';
    
    std::cout << "Play again? [y/N]: ";
    std::cin >> ans;

    if(std::tolower(ans) == 'y') {
        return true;
    }
            
    system("clear");
    return false;
}

One thing that I noticed was the return value of Again is either a 0 (false) or 1 (true), which confused me at first since I was originally checking doAgain to either be true or false.

Nothing complicated, but still interesting and fun! These programs can be found on my ptp repository.

hello_js.js

April 22, 2020
Hello World JavaScript

hello_c.c

April 20, 2020
Hello World C

Doom 0

September 22, 2019
DOOM C C++ Linux Open Source
comments powered by Disqus