What's new

Closed C++ for beginners

Status
Not open for further replies.

PHC - KiNG

Addict
Joined
Aug 12, 2017
Posts
100
Reaction
24
Points
88
A few thing I have to say before the tutorial begins:

- I will try to use at least 2 different ways to do a code.
- I will avoid using "using namespace std" as more complicated stuff will make using this impossible.
- Make sure to read explanations, being a leecher won't help you.
- Write these codes while you read, play around a bit with them too, that's how you learn.


Simple Calculations:

So the first thing you need to learn is to make some easy calculations such as addition, multiplication, division, square root, etc. So we will start with some basic includes:

Code:
#include <iostream>
#include <string.h>

So all of the calculation stuff (and much more, obviously), exist in the iostream library.
The string library will basically allow us to define strings, which is basically any form of text.
i.e.: "d1f2432wt23wg%##FRTT##t2gw34" is a string.

Code:
int main(){

Here the code basically starts. We are doing calculations, remember, so we will have to say it's an int (integer a.k.a. number), we will find out about more on this in another tutorial.

Code:
int a, b;

Here we're defining some integers that will be given a value by you and will be made calculations with. You can name them whatever you want, but remember to put a comma (",") between them and obviously a semicolon (";") at the end.

Code:
std::cout << "Give the integers their values: ";

Remember, we did not add the std at the beginning of our program so we'll have to write it in the code, like here, for cout. Cout basically means that it will print a text on the screen, hence the name c + out. We are printing the text "Give the integers their values: " which has to be between quotation marks(").

Code:
std::cin >> a << ", " >> b;

Now you are giving the integers a and b a value and because they are integers, that value will be a number.
Like, I said, I will give you an alternative to cin, though I don't know if it actually matters but here you go:

Spoiler contents are visible only to Established Members.


>> - Used by cin for giving a value to that integer (can be a string too, but you'll see that later) and also used by cout if you want to print that integer's value (after you gave it one with cin of course, but you can also do that without cin: i.e.: int a = 4;).

<< - Used for printing text by cout mainly.

The output until now will be (what you will see after you compile this program):

Spoiler contents are visible only to Established Members.


Before we move on, I want to tell you about std::endl, which is exactly what it says, end line, it ends the current line and moves to the other. For example:

Code:
std::cout << "line1" << std::endl;
std::cout << "line2";

The output will be:

Code:
line1
line2

You can also use "\n" for this, as in:

Code:
std::cout << "line1\nline2";

Which will also output:

Code:
line1
line2

So now you want to do some actual calculations, let's do all at once.

So let's say:

a = 4.
b = 6.


Code:
std::cout << a + b << std::endl;

This will mean 4 + 6 which is 10.

Code:
std::cout << a - b << std::endl;

This will mean 4 - 6 which is -2.

Code:
std::cout << a * b << std::endl;

This will mean 4 x 6 which is 24.

Code:
std::cout << a / b << std::endl;

This will mean 4 : 6 which is 0,(6).

Code:
std::cout << sqrt(pow(a,2)) << std::endl;

Because a's value was set to 4, this code means √4²

Code:
std::cout << pow(b,4) << std::endl;

The value of b is 6, this code means 6⁴

Checks:

So you have to learn about if and else statements, which are basically exactly what their name is:

if(a<0) means if a<0 is true

if(!a<0) means if a<0 is false

After this we will open the brackets and we say what happens if that argument is true or false.

else(a>0) means that else the argument that a<0(if(a<0)) we made before is false, for example, !a<0 so a is not smaller than 0, we come with an else expression and say else that isn't smaller than 0, but is bigger than 0, it does something else.

For example we have:

Code:
int a = 5;
std::cout << "Give 'a' a value: ";
std::cin >> a;

We want to check if the value of 'a' that was entered is actually an integer (number). We can do:

Code:
if(std::cin.fail()){
return 0;
}

Which basically checks if the cin of a failed (because of course if you enter 'afg43g34' where you should've entered a number, that makes no sense, so in the case if fails, it returns 0 (closes the program).

There are a bunch of things you can use with if/elsebut it's obviously no one learns them all, if you need something you google it and you'll find it, you don't need to learn anything you basically need to learn how to learn, though there are some things you will need to know:

Spoiler contents are visible only to Established Members.

That's all for this tutorial, I had to make it short as I don't have much time right now, in the next one (hopefully) will be about:

- for
- while
- define
- typedef
- pragma
- vector
 
TS kay deitel ba yan o kay pearson? credits mo nalang kung saang libro mo nakuha. respect na rin natin sa gumawa ng libro para sa mga programmers hehehehe
 
Status
Not open for further replies.

Similar threads

Back
Top