Hello there, I see you've decided to keep going, thats's good!
As much as I've said I don't care much about theory, these are a few basic things that tutorials I found never taught me, I've had to figure them out by myself when I was trying more complex stuff. I wish I had this back then.
<h3>This will probably be a bit boring but I promise you it will save you from a lot of headaches in the future!
<p dir="auto">Everything in quotes are my own experiences and real life situations which you can skip if you're a TLDR kind of guy.<br />
Let's first understand variables for real this time.
<h2>Variables
<p dir="auto">char = Stores exactly one character this is meant for (a-z)<br />
int = Stores a number, remember it has a minimum value and a maximum value, so don't use this for numbers larger than 2 bilion or -2 bilion.
<blockquote>
<p dir="auto">For example, on an online php text game long ago, a very easy way to cheat in-game money was inputting -3000000000 and it would reset to giving you 2.5 Bilion in-game currency and this is why you should know this for any programming language. Otherwise you will have unfortunate surprises.<br />
(Yes I did cheat on it! =)) )
<p dir="auto">Float = High precision numbers Numbers that also have decimals;<br />
Double = Same, A high precision number;<br />
float & double are High Precision numbers, this can store higher numbers than int, and more precise because it also calculates values such as 23.0005 if you set it to that precision in case you want to test this out [cout << setprecision(5) << fvar; where 5 represents the number of decimals aka: 23.(0005) = precision is set to 4<br />
For better understanding you can document yourself here: <a href="http://www.cplusplus.com/reference/limits/numeric_limits/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">numeric limits<br />
Phew, got past that little detail that everyone seems to forget, that they do have limits, and if you want to use extremely large numbers you have to use libraries such as <a href="https://en.wikipedia.org/wiki/GNU_Multiple_Precision_Arithmetic_Library" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">GMP
<pre><code>#include <iostream>
using namespace std;
int main()
{
float a=1.22;
double b=2;
int i=1;
cout << "The result is:" << a*b+c << endl; //Remember endl or "\n" are the same thing.
system("pause");
}
<p dir="auto">Wow what the hell did I do there. I just used float and double and int together?<br />
Yeah you can do that, except I strongly advice you don't. You should work with one variable type for one calculation.<br />
It will most likely save you a lot of trouble yet you should be aware that you can also do this. Tutorials usually hide it from you a lot so you don't screw up.<br />
Now let's talk about setting the precision for this you will need the <a href="http://www.cplusplus.com/reference/iomanip/setprecision/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">iomanip library!
<pre><code>#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a = 2.1;
float b = 5;
float c = b/a;
cout << "b/a = " << c << setprecision(5) << endl; //setprecision = sets the number of decimals to display
system("pause");
}
<p dir="auto">Let's move on to the <a href="http://www.cplusplus.com/reference/cmath/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">math library
<h1>Math-Calculus Yes I hate it too!
<p dir="auto"><img src="https://images.hive.blog/768x0/http://s2.quickmeme.com/img/7c/7cace54ced07a4649c460b72ec11c7f6537fb5276a5fdd75628e27699fc73a8f.jpg" alt="img" srcset="https://images.hive.blog/768x0/http://s2.quickmeme.com/img/7c/7cace54ced07a4649c460b72ec11c7f6537fb5276a5fdd75628e27699fc73a8f.jpg 1x, https://images.hive.blog/1536x0/http://s2.quickmeme.com/img/7c/7cace54ced07a4649c460b72ec11c7f6537fb5276a5fdd75628e27699fc73a8f.jpg 2x" />
<blockquote>
<p dir="auto">Yes, I know math seems horrible and schools scare their students with math math math, but to be honest unless you're going to make the architecture of an processor or go into more complex stuff yeah you sure as hell need a lot of math if not, and you just want to make common software, and not get into 3D arithmetics and all then you can be pretty stupid with math and still do pretty well.<br />
For example my first real job, which was easy since I started programming all the way back when I was 10<br />
my first real job required me to make a calculator. No not just any calculator, a calculator for structural engineers that would take a few values and do the math for them. /Wow engineering math eww that must've been complicated as hell.<br />
Well actually no. I was in the second year of highschool and my math was not bad, extremely bad, hell I didn't know anything. I probably forgot a lot of math from school too.<br />
(Yeah if you're wondering this cost me a lot in university when I had to actually learn everything from general school to university level in a year I haven't ever been so sleepless)<br />
Because they give you the formula, and you find the equivalent in programming math functions, and you don't have to know anything but the basic math.
<p dir="auto">I'm going to give you a few examples of how to do the basic things, remember you can always find all the examples of <a href="http://www.cplusplus.com/reference/cmath/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">the math library here in case you actually need to do something more complex. The internet is every programmer's best friend, or in some cases worst enemy.
<pre><code>#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
cout << "A list of functions and how to use them";
cout << "pow()-Power Function: 3^2= " << pow(3, 2) << endl;
cout << "sqrt()-Square Root Function: Square Root of 4 = " << sqrt(4) << endl;
cout << "cbrt()-Cubic Root Function: Cubic Root of 4 = " << cbrt(4) << endl;
cout << "hypot()-Hypotenuse Function: Hypotenuse of 4, 5 = " << hypot(4,5) << endl;
cout << "Rounding Numbers to nearest, lowest, highest examples:\n";
cout << "the number we will use the following functions on is 1.5\n";
float i = 1.5;
cout << "round()-Round to nearest:" << round(i) << endl;// since it's 5 it picks higher value
cout << "round()-Round to lowest :" << floor(i) << endl;
cout << "round()-Round to highest:" << ceil(i) << endl;
system ("pause");
return 0;
}
<p dir="auto">Run it, experiment with it if you will, it's all just formulas being calculated for you.<br />
Or just make your own calculator with std::cin //Don't tell me you've already forgotten how to ask for input!?
<h3>Anyways, enough of that boring math, let's move on to
<h2>String Concatenation
<p dir="auto"><img src="https://images.hive.blog/768x0/http://vignette3.wikia.nocookie.net/icarly/images/0/07/D2047_funny-dog-pictures-oh-god-what-the-hell-is-that.jpg/revision/latest?cb=20120821112712" alt="img" srcset="https://images.hive.blog/768x0/http://vignette3.wikia.nocookie.net/icarly/images/0/07/D2047_funny-dog-pictures-oh-god-what-the-hell-is-that.jpg/revision/latest?cb=20120821112712 1x, https://images.hive.blog/1536x0/http://vignette3.wikia.nocookie.net/icarly/images/0/07/D2047_funny-dog-pictures-oh-god-what-the-hell-is-that.jpg/revision/latest?cb=20120821112712 2x" /><br />
Ha hah, don't worry it's really simple but you might eventually need it you see string concatenation means<br />
String unification or string a + string b = string c for example, or just "John" + " "+ "Doe" = "John Doe"<br />
It's still imperative that you know this.<br />
I'm going to.. forge an example so to speak that could make sense as to why you would do this.
<pre><code>#include <iostream>
#include <string>
using namespace std;
int main()
{
string tld = "com";
string domainname="gmail";
string separator="@";
string user="user";
string email = user+separator+domainname+"."+tld;
cout << email;
system("pause");
}
<h2>Enough about this it's simple enough, let's talk about
<h1>Conversion
<p dir="auto"><img src="https://images.hive.blog/768x0/http://starecat.com/content/wp-content/uploads/pope-francis-wololo-change-color-of-clothes.jpg" alt="img" srcset="https://images.hive.blog/768x0/http://starecat.com/content/wp-content/uploads/pope-francis-wololo-change-color-of-clothes.jpg 1x, https://images.hive.blog/1536x0/http://starecat.com/content/wp-content/uploads/pope-francis-wololo-change-color-of-clothes.jpg 2x" /><br />
No not that kind of conversion I'm talking about <em>variable types conversion.<br />
You will often need to convert your variable types in order for them to work with different functions you use.<br />
When I made my first server-client programs god how I wish I was aware of this back then.
<h3>We're also going to talk about arrays such as:
<p dir="auto">char a[] = "string" in this case the char array has values from 0-5 which means for example if we say:<br />
cout << a[0]; it will output simply the character "s"<br />
since some functions don't use strings you will have to convert them to suit the needs of those functions.<br />
and this is what this chapter is about.<br />
For the array to string conversion you will require the cstring library.<br />
In the following examples, which are pretty self explainatory with added comments to help you out.<br />
I've also encountered an error in my compiler with mingw. I found a quick patch since I can't be bothered to update it right now so just ignore the parts which I highlighted if you don't encounter any errors with it, if you do, just apply the patch as I did.
<pre><code> #include <iostream>
#include <string>
#include <cstring>
using namespace std;
/*DUE TO A MINGW BUG I HAD TO DO THIS, YOU SHOULD NOT NEED TO USE THIS HOWEVER IF YOU HAVE THE SAME VERSION I HAVE
THEN THIS WILL BE NECESARRY, IT'S A CUSTOM PATCH.*/
#include <sstream>
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
using namespace patch;
//END CUSTOM PATCH. THIS IS NOT PART OF YOUR TUTORIAL!
int main()
{
cout << "*CHAR TO STRING*\n";
//Conversion from a char array to string is extremely simple.
char a[] = "HELLO";
cout << "a=" << a << endl;
string message = a;
cout << "message = " << message << endl;
/*However should we need to convert a string to a char array the process becomes a bit tricky.
Since we are talking about c strings which were widely used before the c++ craze you need a bit
of history, when dealing with simple C you need to also worry about memory consumption
(You know, ram and all that, or if you don't, take my word for it) and for this reason we need
to use two functions.
c_str and strcpy.
*/
cout << "*STRING TO CHAR*" << endl;
string str = "HELLO";
char arr[str.size()];
strcpy(arr, str.c_str());
cout << "Array: " << arr << endl;
cout << "Size of array: " << sizeof(arr) << endl;
/*Seems a lot of effort for such a simple thing right?
Either way, this will prove to be useful for you in the future if you plan on working with c++.
I'm certain of that.
*/
cout <<"*Numbers to strings*\n";
//Numbers to strings Yes you will need this too in actual development enviroments
int anint = 100;
string intmessage = "The Value of a is: ";
intmessage = intmessage + to_string(anint);
cout << intmessage << endl;
}
<p dir="auto">Feel free to ask me anything you don't understand in the comments below!
<blockquote>
<p dir="auto">Everything I wrote here is my own personal work.<br />
The First image is of my own design.<br />
List of images that are public domain:<br />
<a href="http://s2.quickmeme.com/img/7c/7cace54ced07a4649c460b72ec11c7f6537fb5276a5fdd75628e27699fc73a8f.jpg" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">TUX<br />
<a href="http://vignette3.wikia.nocookie.net/icarly/images/0/07/D2047_funny-dog-pictures-oh-god-what-the-hell-is-that.jpg/revision/latest?cb=20120821112712" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">DOG<br />
<a href="http://starecat.com/content/wp-content/uploads/pope-francis-wololo-change-color-of-clothes.jpg" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Age of empires monk conversion