(Not So) Quick C++ Tutorial - Make A Cipher!
Highwayman

why am I making this?
because I’m bored plz read me.

I have actually tried to make a C++ tutorial over and over again, but each time I always encountered one horribly simple problem: I didn’t know where to start.
So for this tutorial, things will be different. Through it, you shall learn how to make a very simple cipher.

side note: A big problem I have with this tutorial is that most information I must teach you is too interconnected to easily teach in a sequential manner, so you’ll have to take a lot of info as is and leave the understanding ‘till later in the tutorial. Basically be patient is all I guess?

Let’s begin!

The first very essential thing you must know when writing C++ is that all C++ programs start by running the main function.
The syntax looks like this:

Data Types & Variables

Let’s take a look at this first word: int.
So what is int?
int is a data type. All variables and functions have a data type. The data type of a variable determines what type of operations are performed on that variable, how that data is represented, and a couple other cool stuff.
There are actually a lot of different data types that I could discuss, but there are only a couple of data types that I’ll show you for now:
1. int
2. char
3. double
4. bool

The first type, int, holds any integer under around 2 billion(hence the name ‘int’).

an integer is any number that does not contain a decimal portion. For example 3.55 is not an integer because of the .55. It’s limited to under 2 billion for space reasons. For stuff above that... well we’ll get into that later or you can just look it up.

char holds any ascii character, like 'k' or '$'

Don’t worry yourself too much about what an ascii character exactly is, just look up "ascii character table" if your worried that something isn’t an ascii character. Plenty of stuff is already, so... yeah.
Anyways...

double is any real number under... um. I don’t actually know. 2^52 or something like that.

real numbers may contain a decimal portion, so my previous example of 3.55 would be accepted as a double.

bool can only hold two very special values: true and false

Variables are always declared with their type like so:

Oh! Of course! I almost forgot, heh. All statements in C++ end with a semicolon, so basically, always put a semicolon unless told otherwise..

A variable is just a named spot in memory where you can hold some piece of data. Variables are extremely important to all programs, of course. Once you have declared a variable you can do operations on that variable. The kinds of operations differs between data types. Here’s a little list of operations on the types we have so far:
=

The = operator assigns stuff to some variable like another variable or a value. Like I could assign the value 5 to the the int variable var by coding var = 5. All of the types we know so far can do this operation.

+

The + operator adds things. You use it by placing it between two values or variables like this: 12 + 3.All our types can do this, but for some types it's kind of complicated how this operation is handled, for example a char or a bool is going act a bit strangely when you use this operator. Can you tell me what 'a' + 'b' is? I certainly can't. Nor would I necessarily want to know, seeing as adding characters or bools seems pretty useless!

-

The - subtracts things. You use it just like the + operator. Again, the same thing as above applies: You can do it to all our types, but it's not necessarily helpful or recommended for some.

You probably noticed a pattern in the way you use the given operators:
var = 5
12 + 3
25 - 2
They all follow the format value_or_variable operator value_or_variable! This means that all of these operators fall under the category of binary operators. There a couple other types of operators, like unary or even the ternary operator, but most operators are binary operators. The difference between each of these types of operators is the general format for their use; for example, a binary operator is coded like a operator b, but a unary operator is coded like operator a.

Functions

Ok so now that you know what a data type is, we can look at the rest of this code.

So this is weird. This code looks nothing like a variable declaration! What’s with all these stupid parens and squiggly brackets??
That’s because it’s a function!
A function is a piece or code that can be run any time you want by calling it. You call a function with the format func_name( args... ) where func_name is the name of the function you’re calling and args... is a comma separated list of variables/values that you are passing to the function to use.

Here’s an example:

Hm? What’s happening here??
Well, when I call add2, I passed the argument x, which contains the value 3. The function then adds 2 to the value 3, and returns it. What this basically means is that once the function finishes running, you can visualize the returned value replacing the function call! So the above code ends up looking like this once the function returns:

All functions are declared like

where
RETURN_TYPE is the type of data that the function will return, FUNC_NAME is the name of the function,
ARGS is a comma separated list of arguments in the form data_type arg_name,
CODE is the code that’s run, and
SOMETHING is whatever you wish to return that is of the same type as the return type!

Anyways. Back to the main function!

So now that we know this, we can determine that the main function coded here takes two mysterious arguments, does nothing, and then returns 0! How useless! Let’s make this more interesting.

Conditionals

So now this is a little more interesting! I’ve changed this program to now only return 0 if argc is equal to 3! To do this I used an if-else statement.
An if statement is a piece of code that only runs if a given condition or operation results in true or a non-zero value.
The syntax for an of statement is

Where CONDITION is the operation that should evaluate to true for allowing the code to be run and CODE is the code that is conditionally run.
The operation I used above in the newer complete program was ==, which tests whether or not one variable is equal to another. This is not to be confused with the = operator, which is the assignment operator, and assigns a value to a variable. There are a couple operators that can test for different things.

  • == equal
  • != not equal
  • < less than
  • <= less than or equal to
  • > more than
  • >= more than or equal to

And if you have several conditions or tests to do, you can string them together using c1 && c2 or c1 || c2, where && signifies that both c1 and c2 have to evaluate to true, and || means either c1 or c2 has to be true.
Sometimes, there is an additional piece of code that you want to run if the condition fails. You can tack it on to the end of any if statement as an else statement.

Casting

So now I want to talk about something I said earlier that might have sounded strange:

An if statement is a piece of code that only runs if a given condition or operation results in true or a non-zero value.

Why can it be true or a non-zero value? Well this is because of casting. Basically what casting is is the act of taking a value of one type(say, char), and transforming/interpreting it as a different type! (say, bool).
When an value that is not of the bool type appears as the result in an if statement condition, it is implicitly cast to a bool value, which simply interprets any non-zero value as true and 0 as false. The rules for casting is pretty simple.

  • int drops the decimal portion of a double, takes the ascii value of a char, or makes true into 1 and false into 0.
  • double just takes the int as is and does everything else like an int.
  • char uses the ascii table to get a character from a value. look it up.
  • and bool just makes any non-zero value into true and any 0 into false.

There’s actually several different ways of manually casting one value to another, but for now the way that we will be using is new_type(value), where new_type is the type you are casting to and value is the thing you are casting.

Bash & G++

Now once you click run, you’ll notice that this program is always returning 1, and it doesn’t actually allow you to interact at all! How rude! Well, this is because the arguments passed to main come from when the program was invoked in the shell! The run button automajically does this for you, but you can do it too! :)

1.

First you have to open your shell.

A shell is an interface between you and your computer. The current shell we are using is “bash”.

To do this in a C++ repl on replit, simply click on the little (?) icon in the bottom left. A little prompt box will pop up with a big list of stuff and a place to search. Click there and type "shell". An option should pop up called “open shell”. Click on it. A second terminal should open on your screen with a prompt like this:

2.

Now that you have a shell, you can run your program! To do this, type

This is basically exactly what your run button is doing. let’s break this down.

g++ is an executable. An executable is a type of file that can be run in some way. This particular executable is actually called a compiler.
A compiler generates an executable from a given piece of code. You can think of it as a translator between you and your machine. The C++ programs that we’ve been writing above are not actually executables, but text files that the compiler must translate into an executable.

-o main is a command line option. A command line option is like when you pass variables to a function, so you can envision this as program(option);. This particular option specifies the file name of the produced executable.

This is yet another option. Some options (like this one) don't need a flag like -o to say what they are for. This is simply telling us which text file to compile.

This is the executable file you created! You're running it. Notice the ./ at the beginning of the name, this is telling the shell where exactly your executable file is. You did not have to specify this for g++ because it was in a default directory for executables. the ./ specifies your current working directory.

Ok, so now that you have run the program, you want to figure out what the return value was! this is pretty easy: just type the command

After the main executable has finished, you can type the above command, and you'll find that it still returns

What You Came For

Ok! I think we’ve got all that is needed to understand a good chunk of these code segments. Let’s get it! :P

Ok, first off, let’s make our basic, basic, starting point of all C++ programs.

Ok, awesome. Now, when I make a program, I like to think very carefully about my inputs and outputs.

So, let’s start with the input. A couple questions that came to mind for me:

  • What input do we receive?
  • When do we receive it?
  • How do we receive it?
    So for the first question, I’d say probably the only input we actually request from the user is

NOT FINISHED!!

You are viewing a single comment. View All
CodeLongAndPros

@HahaYes …thanks?