C++: Main
I know that there was already a tutorial on structs, but I wanted to make my own, and go in with some other aspects of C++. First of all, you may ask yourself, why would I want to use structs? Structs are very useful in games. A struct is a point in your C++ file inwhich you may prepare a huge list of variables. Let us take a quick preview of an example struct:
struct stats
{
int health; //Here we prepare a simple health variable. Notice that the type of the variable is an "integer."
int level; //Here we prepare a simple level variable. Also notice, the type of the variable is an "integer."
int exp; //Here we prepare a simple experience level. Once again, notice that the type of the variable is an "integer."
}; //When closing a struct, instead of the traditional closing curley bracket, you have that, as well as a semi-colin, so it flows together as "};."
Now, reviewing that struct, as you can see, it is just a very simple struct. Now, let us take a look at a simple C++ program using structs.
//Here we include all of the header files for the application.
#include <iostream>
//Here we prepare all of the name spaces.
using namespace std; //We need this name space for most console functions, so instead of typing std::cout << "Hello, World!" << endl; we can type cout << "Hello, World! " <<endl;
//Here we setup the structs for in-app use.
struct stats
{
int health;
int level;
int exp;
};
//Here we setup all of the variables for in-game use.
stats player;
stats enemy;
//Here is the main program loop.
void main(){
player.health = 100;
player.level = 1;
player.exp = 10;
enemy.health = 110;
enemy.level = 2;
enemy.exp = 30;
cout<<"Welcome to my program! The stats are:\n-------------------------------------------------------\nPlayer Health: " << player.health << "\nPlayer Level: " << player.level << "\nPlayer EXP: " << player.exp << "\n-------------------------------------------------------\nEnemy Health: " << enemy.health << "\nEnemy Level: " << enemy.level << "\nEnemy EXP: " << enemy.exp << "\n-------------------------------------------------------\nPress enter to exit the application. \n-------------------------------------------------------" << endl;
cin.get(); //Here we pause output until the user preses a generic key.
return; //Here we use "return;" to return out of the program loop. If we were using "int main(){" instead of "void main(){," we would use return 0; instead of return;
}
If you can not stand that cout line, here it is again, but with more line drops:
//Here we include all of the header files for the application.
#include <iostream>
//Here we prepare all of the name spaces.
using namespace std; //We need this name space for most console functions, so instead of typing std::cout << "Hello, World!" << endl; we can type cout << "Hello, World! " <<endl;
//Here we setup the structs for in-app use.
struct stats
{
int health;
int level;
int exp;
};
//Here we setup all of the variables for in-game use.
stats player;
stats enemy;
//Here is the main program loop.
void main(){
player.health = 100;
player.level = 1;
player.exp = 10;
enemy.health = 110;
enemy.level = 2;
enemy.exp = 30;
cout<< "Welcome to my program! The stats are: " << endl;
cout<< "-------------------------------------------------------" << endl;
cout<< "Player Health: " << player.health << endl;
cout<< "Player Level: " << player.level << endl;
cout<< "Player EXP: " << player.exp << endl;
cout<< "-------------------------------------------------------" << endl;
cout<< "Enemy Health: " << enemy.health << endl;
cout<< "Enemy Level: " << enemy.level << endl;
cout<< "Enemy EXP: " << enemy.exp << endl;
cout<< "-------------------------------------------------------" << endl;
cout<< "Press enter to exit the application." << endl;
cout<< "-------------------------------------------------------" << endl;
cin.get(); //Here we pause output until the user preses a generic key.
return; //Here we use "return;" to return out of the program loop. If we were using "int main(){" instead of "void main(){," we would use return 0; instead of return;
}
I personally prefer to use a single line then multipul lines, but whatever suits your needs. Encase you do not know, "\n" is a new line, as well as "endl;"
The above block compiled perfectly using Microsoft Visual C++ 2008.
Now, I will talk about custom functions. Go ahead and made a new header file. I will call mine "customfunctions.h." You may call it whatever you wish, but it is not a good idea to call it "functions.h," as there already may be a header file in your IDE (If you are using one), and you may have name conflicts.
Inside of your "customfunctions.h," (or whatever you called it), add this block:
#include <iostream> //Our major header file we need to use for consoles.
using namespace std; //Our "std" name space we need to use to shorten lines.
//On to the functions:
void simpleAddition(int num1, int num2){
cout << num1 + num2;
return;
}
//The above makes a function entitled "simpleAddition," with two prameterers, "num1" and "num2." The program will add both priamters.
//We use return; to continue on with your program.
Now, make a "main.cpp" file. Inside of your main.cpp file, add this.
//Here we prepare the header files we need to use.
#include "customfunctions.h"
//We only need to include "customfunctions.h," as it already has iostream and the namespace prepared inside of it.
//Here we prepare the struct for use:
struct mathTest
{
int number1;
int number2;
};
//Here we prepare the variables for use.
mathTest numbers;
//Here is the program loop.
void main(){
numbers.number1 = 2;
numbers.number2 = 2;
cout<< "The answer to " << numbers.number1 << " + " << numbers.number2 << " is ";
simpleAddition(numbers.number2, numbers.number2);
cout<< "." <<endl;
cout<< "Press enter to exit the application." <<endl;
cin.get();
return;
}
That compiled perfectly using Microsoft Visual C++ 2008.
I really do not think that there is anything else to go over. I hoped you learned something in this tutorial. Opinions?