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.
This will probably be a bit boring but I promise you it will save you from a lot of headaches in the future!
Everything in quotes are my own experiences and real life situations which you can skip if you're a TLDR kind of guy.
Let's first understand variables for real this time.
Variables
char = Stores exactly one character this is meant for (a-z)
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.
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.
(Yes I did cheat on it! =)) )
Float = High precision numbers Numbers that also have decimals;
Double = Same, A high precision number;
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
For better understanding you can document yourself here: numeric limits
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 GMP
#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");
}
Wow what the hell did I do there. I just used float and double and int together?
Yeah you can do that, except I strongly advice you don't. You should work with one variable type for one calculation.
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.
Now let's talk about setting the precision for this you will need the iomanip library!
#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");
}
Let's move on to the math library
Math-Calculus Yes I hate it too!
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.
For example my first real job, which was easy since I started programming all the way back when I was 10
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.
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.
(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)
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.
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 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.
#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;
}
Run it, experiment with it if you will, it's all just formulas being calculated for you.
Or just make your own calculator with std::cin //Don't tell me you've already forgotten how to ask for input!?
Anyways, enough of that boring math, let's move on to
String Concatenation
Ha hah, don't worry it's really simple but you might eventually need it you see string concatenation means
String unification or string a + string b = string c for example, or just "John" + " "+ "Doe" = "John Doe"
It's still imperative that you know this.
I'm going to.. forge an example so to speak that could make sense as to why you would do this.
#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");
}
Enough about this it's simple enough, let's talk about
Conversion
No not that kind of conversion I'm talking about variable types conversion.
You will often need to convert your variable types in order for them to work with different functions you use.
When I made my first server-client programs god how I wish I was aware of this back then.
We're also going to talk about arrays such as:
char a[] = "string" in this case the char array has values from 0-5 which means for example if we say:
cout << a[0]; it will output simply the character "s"
since some functions don't use strings you will have to convert them to suit the needs of those functions.
and this is what this chapter is about.
For the array to string conversion you will require the cstring library.
In the following examples, which are pretty self explainatory with added comments to help you out.
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.
#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;
}
Feel free to ask me anything you don't understand in the comments below!
Everything I wrote here is my own personal work.
The First image is of my own design.
List of images that are public domain:
TUX
DOG
Age of empires monk conversion