Learn to Code via Tutorials on Repl.it!

← Back to all posts
(Not So) Quick C++ Tutorial - Make A Cipher!

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:

``````int main(int argc, char ** argv)
{
return 0;
}``````

# 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:

``data_type var_name ;``

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:
1. //...
2. //...
3. //...

# Functions

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

``````int main(int argc, char ** argv)
{
return 0;
}``````

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:

``````int x = 3;
x = add2(x); ``````

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 `return`s 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:

``````int x = 3;
x = 5;``````

All functions are declared like

``````RETURN_TYPE FUNC_NAME( ARGS... )
{
CODE
return SOMETHING ;
}``````

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!

``````int main( int argc, char ** argv)
{
return 0;
}``````

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

``````int main( int argc, char ** argv )
{
if(argc == 3)
{
return 0;
}
else
{
return 1;
}
}``````

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

``````if ( CONDITION )
{
CODE
}``````

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:

``[email protected]\$ ``

#### 2.

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

``````g++ -o main main.cpp
./main``````

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

``g++``

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``

`-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.

``main.cpp``

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.

``./main``

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.

# 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.

``````int main(int argc, char **  argv)
{
// ...
}``````

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!!

WilliamWu13 (0)

HahaYes..... what about me

CodeLongAndPros (1535)

Wow, the ending is like Firefly

HahaYes (1284)

@CodeLongAndPros CODELONG YOU ARE A MOD CONGRATS

DynamicSquid (4534)

I should make an absolute banger of a C++ tutorial Cool!

Highwayman (1439)

YES DO IT! :P I know there’s a lot of of c++ tutorials in replit but I just enjoy them too much lol.
@DynamicSquid

DynamicSquid (4534)

@Highwayman dude I'm so happy, my answer just got 2 upvotes on stack overflow

Highwayman (1439)

noiiice what was the question? Can I have link? :[email protected]

HahaYes (1284)

@DynamicSquid yes, C++ tutorials are the best

Highwayman (1439)

# YEAS!

Highwayman (1439)

Darn it all! I didn’t have time to finish lol. Will fix that..

EpicGamer007 (1241)

@Highwayman, great! Looking forward to it!

Just a suggestion, but maybe next time, finish the tutorial before posting it.

Highwayman (1439)

Lol yeah... XD
I was in the middle but some extremely pressing stuff came up, and I had to put down the phone, so I had to just submit so I didn’t lose all my progress. XP @AbhayBhat

Highwayman (1439)

I’ll probably delete and repost it when it’s done or something... @AbhayBhat

ApoorvAgrawal (49)

@Highwayman maybe you should make the tutorial inside a .md folder, and then when you're finished, copy and paste inside to post.

Highwayman (1439)

Yeah... I should have, but I thought I had plenty of time to just finish it up right in the post place so I was just doing it there... :( I failed of course XD now this thing is gonna be like partial teaser release or something lol. @ApoorvAgrawal