=The skeleton of a c++ program=
#include < iostream > //include statements goes here
using namespace std; //using statements goes here, std = standard
const int num=1; //declare any constant variables here
int main() //the main function of a program
{
//declare any variables if needed
/* this is the main body of a program, this is where you want to put all of your coding for your program, between the {}s, by the way, put it before the statement return 0.
*/
return 0;
}
#include < iostream > - it tells the compiler to include the file, there is a difference between <> and “” like “stdafx.h” in vc++. You don’t have to put include statements into stdafx.h, but it makes the main cpp file less messy. <> tells the compiler to look into the main library for the header file. “” tells the compiler to include the header file where your cpp file is stored.
using namespace std; - so you don’t have to use the std:: prefix when you want to use cout, endl, ect. It makes your life easier.
const – declare a constant variable, I never const, since I don’t see a point in it, but const variables’ data never changes. If it is 1, it stays 1 throughout the program.
int main() {} - the main function of your program. This is the brain of your program, all of your coding goes in between the brackets.
return 0; - returns a 0 to the function to end the program. You can return whatever value you like, you can even return one million. If you return a value other than a 0, it means that the program ended because of an error instead of a normal termination.
; tells the compiler this is the end of a command.
----------------------------------------------------------------------------------------------------------
=Variable Type That You’ll Use=
int var; - declares a variable called var, since its int, so it won’t take any decimals
double var; - declares a variable called var, double means real number
long double var; - same as double var; statement, but it can store a larger number
char var; - a variable to store a character
string var; - a variable to store more than one character, you can also use arrays
----------------------------------------------------------------------------------------------------------
Note: don’t ever declare variables that have the same name like what I did, or it will give you some kind of error, unless if you declare it in another function, which you will learn later on.
A simple Hello World program
#include < iostream >
using namespace std;
int main()
{
cout << “Hello World”;
system(“pause”);
return 0;
}
cout – a function used to output statements
<< - tells the computer to throw string Hello World inside the “”s to the cout
system(“pause”); - pause the program at certain line, I personally gets annoyed by the Press enter to continue or something like that. I use getchar(), it’s a bit more typing if you use it, but it doesn’t have the press whatever crap.
Ok now, let’s get familiar with the input command cin (I don’t pronounce cin as c-in, I pronounce it as sin, like that button on your calculator)
#include
using namespace std;
int main()
{
int a; //a variable to store the input
cout << “Enter a number: “; //a prompt line to tell the user to input a num
cin >> a; // inputs the num the user enters to variable a
cout << “You entered: “ << a; // outputs the number the user had entered
system(“pause”);
return 0;
}
If it is kinda hard to read, then download the word document instead http://www.mediafire.com/file/ylzj4wammqn/basics.docx
#include < iostream > //include statements goes here
using namespace std; //using statements goes here, std = standard
const int num=1; //declare any constant variables here
int main() //the main function of a program
{
//declare any variables if needed
/* this is the main body of a program, this is where you want to put all of your coding for your program, between the {}s, by the way, put it before the statement return 0.
*/
return 0;
}
#include < iostream > - it tells the compiler to include the file, there is a difference between <> and “” like “stdafx.h” in vc++. You don’t have to put include statements into stdafx.h, but it makes the main cpp file less messy. <> tells the compiler to look into the main library for the header file. “” tells the compiler to include the header file where your cpp file is stored.
using namespace std; - so you don’t have to use the std:: prefix when you want to use cout, endl, ect. It makes your life easier.
const – declare a constant variable, I never const, since I don’t see a point in it, but const variables’ data never changes. If it is 1, it stays 1 throughout the program.
int main() {} - the main function of your program. This is the brain of your program, all of your coding goes in between the brackets.
return 0; - returns a 0 to the function to end the program. You can return whatever value you like, you can even return one million. If you return a value other than a 0, it means that the program ended because of an error instead of a normal termination.
; tells the compiler this is the end of a command.
----------------------------------------------------------------------------------------------------------
=Variable Type That You’ll Use=
int var; - declares a variable called var, since its int, so it won’t take any decimals
double var; - declares a variable called var, double means real number
long double var; - same as double var; statement, but it can store a larger number
char var; - a variable to store a character
string var; - a variable to store more than one character, you can also use arrays
----------------------------------------------------------------------------------------------------------
Note: don’t ever declare variables that have the same name like what I did, or it will give you some kind of error, unless if you declare it in another function, which you will learn later on.
A simple Hello World program
#include < iostream >
using namespace std;
int main()
{
cout << “Hello World”;
system(“pause”);
return 0;
}
cout – a function used to output statements
<< - tells the computer to throw string Hello World inside the “”s to the cout
system(“pause”); - pause the program at certain line, I personally gets annoyed by the Press enter to continue or something like that. I use getchar(), it’s a bit more typing if you use it, but it doesn’t have the press whatever crap.
Ok now, let’s get familiar with the input command cin (I don’t pronounce cin as c-in, I pronounce it as sin, like that button on your calculator)
#include
using namespace std;
int main()
{
int a; //a variable to store the input
cout << “Enter a number: “; //a prompt line to tell the user to input a num
cin >> a; // inputs the num the user enters to variable a
cout << “You entered: “ << a; // outputs the number the user had entered
system(“pause”);
return 0;
}
If it is kinda hard to read, then download the word document instead http://www.mediafire.com/file/ylzj4wammqn/basics.docx
Last edited by GroupZero on Tue Oct 13, 2009 5:13 pm; edited 1 time in total