well. This is my first tutorial on c++, normally I can write good tutorials but it is my first on this topic so it may not be the best.
The code for the simple text based game we will be creating is as follows
in a console application I have created the following two files with the following code within them.
functions.h[php]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void Felse(){
cout << "that wasn't an answer...\n";
}
void test (string a, char c[40], char e[40] ){
if(a == "y"){
cout << c << endl;
}
else if(a == "n"){
cout << e << endl;
}
else { Felse(); }
}[/php]
functions.cpp
[php]#include "functions.h"
int main(){
string b;
char d[40] = "you answered yes";
char f[40] = "you answered no";
cout << "please enter y/n"<< endl;
getline (cin,a);
test(a, c, e);
}[/php]
If you put these in your compiler, it will be a VERY simple text based game.
If this makes sense to you, you shouldn't be reading this. If it doesn't make sense, well you came to the right place. I hope.
Lets take this apart, piece by piece.
The first things we notice in functions.h is [php]#include <iostream>
#include <string>
#include <sstream>[/php]
What this is saying, is to include the iostream.h file which includes input/output streams (cout/cin etc)
to include string, so we can use the string variable which is quite handy and to include sstream so we can use the getline (cin,a); which is far less glitchy the cin >>
as we continue down the code we see [php]using namespace std;[/php]
this handy piece of code makes it so we don't have to add std:: before just about everything. I like that.
Now we get to the confusing part of it... [php]void Felse(){
cout << "that wasn't an answer...\n";
}[/php]
Well, if this were to be void Felse; it would just be a variable with no type declared. But, it's a function with no type declaredbecause its void Felse(){}
what this does, is it makes the code within the '{' '}' run whenever you call Felse();
which MEANS when we calle Felse(); it will basically be replaced by cout <<"that wasn't an answer...\n";
and THAT is what functions is all about (kinda)
[php]void test (string a, char c[40], char e[40] ){
if(a == "y"){
cout << c << endl;
}
else if(a == "n"){
cout << e << endl;
}
else { Felse(); }
}[/php]
now, its void test(string a, char c[40], char e[40]){} now, the strings and chars inside the () are replaced by whatever variables we use when we call this function in our code... more on that later.
Then we have the basic if statements, and what have you.
What you may or may not have noticed, is [php]else { Felse(); }[/php] Yes, thats right. I didn't wanna right cout <<... so I used the Felse function. Really handy and cleans up the code a lot.
Alright, now to expain functions.cpp
[php]#include "functions.h"
int main(){
string b;
char d[40] = "you answered yes";
char f[40] = "you answered no";
cout << "please enter y/n"<< endl;
getline (cin,a);
test(a, c, e);
}[/php]
You may be wondering, WHAT THE HELL1!?!?! but don't worry.
I included function test.h which included everything else we need (chain reaction type thing) By including our own header file, the .cpp file now contains all the code from that header file. Which means, we can use the functions in our header file in our .cpp file. Make sense?
Well, I basically have three variables, a string and two chars. char c is "you answered yes"
char e is "you answered no"
string a has nothing assigned to it yet.
We then use the cout function to display enter y/n then the getline function to get whatever was on that line, take that and put it into a.
get line works a little like cin, just a bit differently. It only works for strings, so you can't do
[php]
int a;
getline (cin,a);
/have to do
string a;
getline (cin,a);
[/php]
and it just inputs whatever was on the line into the variable. quite simple really, not as buggy as cin.
weeellll now we arrive at the functions.
[php]
test(b, d, f);
[/php]
remember how we made the function
[php]
void test (string a, char c[40], char e[40] )
[/php]
well, see how they both have three variables within the brackets? thats important.
when you call a function, whatever is in the brackets replaces any other instance of what is in the brackets when you create the function.
soo, by using test(b,d,f);
we are changing the
[php]
void test (string a, char c[40], char e[40] ){
if(a == "y"){
cout << c << endl;
}
else if(a == "n"){
cout << e << endl;
}
else { Felse(); }
}
[/php]
function too
[php]
void test (string b, char d[40], char f[40] ){
if(b == "y"){
cout << d << endl;
}
else if(b == "n"){
cout << f << endl;
}
else { Felse(); }
}
[/php]
If this STILL does not make sense, please PM me and I'll explain further.
Please, if there is any way I could clean this up please PLEASE tell me. I want to help, not confuse.