Introduction to Floats and Doubles: C++ Crash Course (Free Tutorial)

Have you ever needed to represent numbers with extreme precision in C++? Well, this is the article for you! In this tutorial, you will learn all about floats and doubles, which add decimal places onto the ends of integers. Thus, you can be more precise when trying to represent numbers.

If you are a beginner and want to learn to code from scratch, check out our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Let’s take a look at an example. If you feel like following along in real time, go on over to rextester.com. This site allows you to code in the main window, and test out your code instantly.

Start off by declaring an integer with a value of 5:

int i = 5;

In rextester.com, your main coding screen should look like this:

//g++ 4.9.3

#include <iostream>

int i = 5;
int main ()
{
std:cout << "Hello, world!\n";
}

In order to test our code, replace the "Hello, world!\n" with i. Scroll down to the bottom and press “Run It”.

You will see the number “5” appear at the bottom of the screen.

 

Now let’s see what happens when we try to add a decimal place onto our integer.

Change int i = 5; to int i = 5.5; and run the code.

As you can see, the console only prints out the decimal “5”. It ignores the decimal because integers are not designed to recognize the decimal place. This is where floats and doubles come in.

Below your integer, declare a float of the name j and a double of the name k:

float j;
double k; 

Give the float a value of 5.4, and the double a value of 5.3:

float j = 5.4;
double k = 5.3; 

This time, we want to output not just i, but j and k as well. To do so, add standards for the j and k. Note: add standard end alls to the end of each of the three lines in order to ensure that you start on a new line.

std:cout << i << std::endl; 
std:cout << j << std::endl; 
std:cout << k << std::endl;

As you can see by running the code, the double and the float both represent decimal places. They both recognize that there is one decimal place after the numbers.

Let’s see how many decimal places these two can recognize. As an example, change 5.4 to 5.444444444444 and 5.3 to 5.333333333333.

If you scroll down and run the code, you can see that the compiler only recognizes the code to 5 decimal places, and it’s the same for both floats and doubles. This is only characteristic of this compiler. In some other compilers, doubles can contain twice the number of decimal places as floats can. However, this is only necessary with extremely large data sets where you need extreme precision.

In everyday coding, the difference is pretty negligible.

Want to learn more? Sign up for our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes. Happy coding and see you soon!

Introduction to Strings: C++ Crash Course (Free Tutorial)

Welcome to one of the most important articles you’ll read as a beginner coder!  When it comes to coding, strings are a very important concept, no matter what programming language you’re using. Think of strings as a displayed message. This message can be virtually any sequence of characters from your keyboard, such as letters, numbers, and symbols.

If you are a beginner and want to learn to code from scratch, check out our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Let’s start off with a basic string. If you want to test out some coding in real time, hop over to rextester.com. This website will allow you to code in the main window, and test it out instantly. Type in the following:

std::string

Lets break down this piece of code. The std:: means that the program is drawing from the standard C++ library. You’ll have to use this syntax every time you declare a string. The string means that this particular piece of code is going to be a string.

At this point, we really haven’t done much. We should give the string a name:

std::string my_string

The string now has a name! Now, this still doesn’t actually do anything yet. The compiler will just know that a certain string, my_string, exists. It can now be referred to later in the code.

In order for this string to be really useful, we have to assign in a value. Let’s do that.

std::string my_string = "mammoth interactive";

Now, the string has the attached value of “mammoth interactive”. Any time my_string is printed, it will print the phrase “mammoth interactive”. Congratulations! You just declared your first string.

In rextester.com, the main coding screen should have something like this in it:

//g++ 4.9.3

#include <iostream>\

int main ()

{

std:cout << "Hello, world!\n";

In order to test our string, replace the "Hello, world!\n" with my_string. Scroll down to the bottom and press “Run It”.

You will see the text “mammoth interactive” come out.

Want to learn more? Sign up for our FREE 30-minute introduction course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes. Happy coding!

Learn how to make HTML5 games on sale for 99 dollars

Here is a fantastic interview about one of our courses. In this course you learn how to build, produce and release games which includes setting up the game logic, art and music. This course has been such a success and we are proud to have it available to you.  You can take this course for 99 dollars.
https://www.udemy.com/learn-how-to-make-html-5-facebook-chrome-store-games-and-more/?couponCode=99

See what Jay Salton had to say about the course!

http://neverstoplearning.udemy.com/jay-salton/?tc=email.email_promo.email_promo_contest.2014-05-06_NSL

I made a Chrome store game in a week

Share this story!

Jay Salton studies digital media and runs a small web design business in Brisbane, Australia. He wanted to expand his business into game development but didn’t know how to get started. He discovered Learn to make HTML 5, Facebook, Chrome Store games and more and designed and published a Chrome store game in a week.

jay_salton_on_macbook

Nice to meet you, Jay. Tell me about yourself and the course you took.

I live in Brisbane, Australia. I’m 27 and I’m currently studying a diploma of interactive digital media. I also do a bit of web design in a business I have as well.

Very cool. What are you driving at? What are some of your goals?

I want to develop my business more and go into games development. I’d like to learn to build Android games, like puzzle games, scrolling shooters and stuff like that.

I found Udemy on Appsumo. They had a promotion for HTML5 game course and that’s how I came across it. I enrolled into the course and it took me a little bit to really get into it.

It took me about a week to make the game that I made.

That’s quite fast.

No. It was quite easy to follow. The steps were laid out properly in an order that I could understand. It’s also good that it’s available whenever you need it. You don’t have to go to a class at a certain time. You just watch it whenever you’re available, whenever you’re free.

I was taking the course at night. I spent a lot of late nights on it (chuckles) over a week. I still have a bit to go through yet. The platform game part.

Did you end up building the same game as the course?

I did build the same type of game but using different graphics and different characters. I created my background myself and then I got some friends to do the characters in the game. The Koala and the pelicans. I made the pelicans so the beak was moving and trying to snap the Koala.

Nice. Tell me more about your game.

Ok, so you’re a Koala and you’ve got a jetpack on and you’re sort of flying along and these pelicans are trying to attack you.

The first weapon you start out with is a spear. Once you pick up a power pack you get three spears that shoot out like they spray out. Then you get the water beam, which is sort of like a laser beam but it’s actually a jet of water that takes out the Pelicans. Then the weapon to get is a boomarang. It’s sort of like a honing missile, it automatically hits your enemies when you fire it.



There’s currently 460 people who have installed it and I get quite a lot of traffic on the site as well. Normally around a 1000 visitors per month. Last month almost 2000. I wish there were even more but people are playing it. Someone finally beat my high score. All my high scores were in there from testing (laughs). It feels good to have people out there testing it and using it.

Ha. Very cool. I don’t know why but that sounds hard to me. It sounds hard to build. Just because I don’t have any experience.

Following the steps it was easy. I probably could not have done it without Udemy. Without following that course, I wouldn’t have been able to think of how to do it. But it’s fairly easy, he shows you this game engine which is called Construct 2. So you build it in Construct 2. It’s like Actionscript programming that would be in flash but it actually make HTML5 games.

Then you can put it on different platforms. It’s not just on the web, you can actually make games for phones. It’s less programming with Construct 2, you build it in blocks and each block has an action like if enemies hit it’ll make a sound and get 10 points. Whenever you do an action in this list, run this actionscript (an action sheet).

And you’re also studying this in a formal way as well?

I’m studying web design and maybe I’ll get to do some game development but that will be javascript which is a little harder. But that’s not til later in the year. It’s mostly web design and working with clients for right now.

Any recommendations or tips for someone who is taking the course or trying to get into something similar?

Stick to it. It does take a bit of time to sort of catch onto this stuff. It does take a while but it’s well worth it.

 

Update: Since we first published this story, Jay has finished creating and publishing his first Android game.