Discovering C++

Discovering C++

I have recently fallen in love with C++. So much so that I have decided to switch gears from a web developer to focus solely on C++. I am still keeping my other Jamstack projects current. However, instead of spending the lion's share of my time debugging RedwoodJS, GatsbyJS, TailwindCSS, Deno issues I am focusing 90% of my energy on learning C++.

So far this month I have onboarded a lot of information from various sources. Currently, I have:

  • Completed the Codecademy C++ course (within the 7-day window so I did not have to pay $200+ for a subscription).

  • In the process of reading two books "How Not To Program in C++" && "Learn C++ Quickly".

  • Just over 33.3333% through the excellent site LearnCPP.com

  • Compiling and studying flashcards of essential C++ syntax and terminology ( NeuraCache is a great iOS flashcard app that syncs with Evernote).

It has been a very fun journey, I literally dream at night about C++ and will wake up in the middle of the night just to think about how to solve a pesky C++ bug. While I was going through the "Control Flow" chapter in LearnCPP.com a lot of it was going over my head. I was tempted to just move on and figure it out later. However, when I skipped past it I felt guilty like I was cheating myself. So, I pushed pause and began to focus solely on coding a lot do-while, for, if/else loops. It was then that I got stuck on a program where I was trying to find prime numbers in an array.

After a solid half-day trying to figure it out I went to Stack Overflow for assistance. Posting on Stack Overflow scares me a bit. Getting downvoted or having your question closed stinks - getting filleted alive by experts for all to see on the internet really stinks.

Well sometimes I guess that is the best way to learn because while my question was closed (maybe I could have worded it differently?) I did get some great pointers, which helped me to eventually get the program working correctly.

During the process, I learned a lot too, which is what I was really going for. Here is the final code (see it live ):

constants.h

#pragma once
#ifndef CONSTANTS_H

#include <string>

namespace constants {

    constexpr int randomOne[12] = { 1,2,3,5,7,9,12,13,15,18,23,87 };
    constexpr int randomTwo[12] = { 2,4,6,8,11,13,17,22,43,1,317,822 };

}

#endif // !CONSTANTS_H

main.cpp

#include <iostream>
#include "constants.h"

int isPrime(int x) 
{
    int count = 0;

    for (int j = 1; j <= x; ++j) 
    {
        if (x % j == 0) 
        {
            count++;
        }
    }
    if (count == 2)
    {
        std::cout << x << " is prime.\n";
    }
    else
        std::cout << x << " is NOT prime.\n";

    return 0;
}

int main() 
{
    for (int i{ 0 }; i <= 11; ++i) 
    {
        isPrime(constants::randomOne[i]);                
    }    
}