10-04-2009, 06:05 PM
First of all you will require a C++ compiler; a compiler takes your code and converts to machine code so it can be executed by the operating system. To write the code you will require a text editor, luckily you can download/purchase an IDE (integrated development environment) which has a text editor and a compiler built into one. I recommend either Dev C++ (free) or Microsoft Visual C++ Express Edition (free) to begin with. Once you are feeling more confident with programming i would recommend Microsoft Visual Studio as it is an IDE for multiple languages (e.g. C++, VB, C#, J# and web development) and it includes some includes and libraries not included with the express edition. So what are you going to learn in this tutorial? Well we will be introducing the basics of a C++ program. It won’t do anything special except write a string of text to the console window. However, we will introduce how to retrieve and use user input. So let’s begin with our first program that will output the following string to the console window “Hello World!â€, and then wait for the user to press a key
Open up the IDE of your choice and create a new file with the .cpp extension, call it what you want for the purpose of this tutorial I will call it HelloWorld.cpp. You should now have a blank document open, so let’s start coding.
So when you write a application in C++ and you want to add comments for later reference it can be easily done with two forwards slashes.
So let’s actually write some C++ code, our first line in our HelloWorld application is:
This simply tells the C++ compiler to copy the contents of the file iostream into our application so we can use its functions. Include statements must go at the start of your program so that they can be used later in the program. Iostream is the basic input/output operations for C++ it allows you to write strings to the console window and also receive input from the user of the application.
Our next line of code goes directly below the #include statement:
This line of code tells the compiler that our application will be using the standard library. Using this line of code allows us to simply write cout instead of std::cout, most people do not use this line of code which is a bad habit as it namespace pollution. Make sure you end the line of code with a semicolon (;) as it separates out your statements.
The next line goes below the last line:
The int is short for Integral Type and is 32 bits in size. The C++ language specifies that the main has to return int. The main() tells the compiler that our program should start executing here. The curly brace after the two parenthesis tells the compiler that the main() starts here.
The next line will actually be the line which outputs the string to the console window:
The cout is the standard output stream used by C++, the two less than signs is the insertion operator. Next you enter your string between two quotation marks “Exampleâ€, then call the insertion operator again (<<). Finally after the insertion operator you write endl which is short for end line which you may have guessed ends the line so no more text is written to that line, you write << endl so you don’t have to clutter up your string with \n which does the same thing.
The next line is placed directly below your cout statement:
This after the string is outputted to the screen waits for the user to press a key then shuts down the application. In other tutorials you may have seen system(“PAUSEâ€); which does the same as cin.ignore(); but says: Press any key to continue..., system calls are not a good thing to use. Also the cin.ignore(); doesn’t present you with a message which makes a nicer cleaner program.
The final line of code we use in this main() section is:
This line ends the main() function, returning a value of 0 means that there was no expected errors.
After the return 0; line add the finally piece of code:
This tells the compiler that the main() section of code is finished.
Here is the complete source code:
So if you copy/rewrite that into your C++ IDE then press compile and run or debug it will compile your code and turn it into an executable.
So now that we have created our first program that uses the output section of iostream include, let’s get some user input and display a message using the user input. So using our code from above place the following line after int main(){ and before cout << ... :
As mentioned before the int is a data type and stores a 32 bit value. Next there is the word age which is a variable name we are going to be using for when we retrieve and use user input (make sure you end the statement with a semicolon).
So now let’s use our age variable to retrieve an age from the user, first change the cout << string to a question the user can answer for the purpose of this tutorial the string will be “How old are you?â€:
This is the changed cout << statement.
So let’s get the users input after your cout << statement add this line of code:
This line of code makes use of the iostream include and because we are using the standard library namespace we can just write cin. The cin parts asks the user to define what they should define age as e.g. (33). Like the cout statement you need the insertion operator, however for getting user input you use greater than signs rather than less than. After the insertion operator you say what variable you want the user to define and then end with a semicolon.
Add the following line under your cin line:
This uses the cout function and the insertion operator as used by our previous cout statement and also a string. However, unlike before you have the variable name surrounded by insertion operators then another string. The insertion operators basically take the value of age and place in the output, note: the insertion wrapped variable must be followed by two Ҡor a string e.g. in this case we used a string. The cout ends with the << endl; like before. The cin.ignore(); will not work here as it will end the program prematurely and having nothing will do the same so instead replace
If you know compile and run or debug this program it will ask for the users age, then allow the user to input an age then say how old they are.
Full source code listing:
I hoped this help people in understanding the basics of a C++ program, more lessons on C++ coming soon.
Open up the IDE of your choice and create a new file with the .cpp extension, call it what you want for the purpose of this tutorial I will call it HelloWorld.cpp. You should now have a blank document open, so let’s start coding.
So when you write a application in C++ and you want to add comments for later reference it can be easily done with two forwards slashes.
Code:
// This is a comment anything entered here will not be read by the C++ compiler.
Code:
/*
This is a multiline comment and will also not be read by the C++ compiler.
*/
Code:
#include <iostream>
Our next line of code goes directly below the #include statement:
Code:
using namespace std;
This line of code tells the compiler that our application will be using the standard library. Using this line of code allows us to simply write cout instead of std::cout, most people do not use this line of code which is a bad habit as it namespace pollution. Make sure you end the line of code with a semicolon (;) as it separates out your statements.
The next line goes below the last line:
Code:
int main() {
The next line will actually be the line which outputs the string to the console window:
Code:
cout << “Hello World!†<< endl;
The cout is the standard output stream used by C++, the two less than signs is the insertion operator. Next you enter your string between two quotation marks “Exampleâ€, then call the insertion operator again (<<). Finally after the insertion operator you write endl which is short for end line which you may have guessed ends the line so no more text is written to that line, you write << endl so you don’t have to clutter up your string with \n which does the same thing.
The next line is placed directly below your cout statement:
Code:
cin.ignore();
This after the string is outputted to the screen waits for the user to press a key then shuts down the application. In other tutorials you may have seen system(“PAUSEâ€); which does the same as cin.ignore(); but says: Press any key to continue..., system calls are not a good thing to use. Also the cin.ignore(); doesn’t present you with a message which makes a nicer cleaner program.
The final line of code we use in this main() section is:
Code:
return 0;
This line ends the main() function, returning a value of 0 means that there was no expected errors.
After the return 0; line add the finally piece of code:
Code:
}
This tells the compiler that the main() section of code is finished.
Here is the complete source code:
Code:
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!" << endl;
cin.ignore();
return 0;
}
So now that we have created our first program that uses the output section of iostream include, let’s get some user input and display a message using the user input. So using our code from above place the following line after int main(){ and before cout << ... :
Code:
int age;
So now let’s use our age variable to retrieve an age from the user, first change the cout << string to a question the user can answer for the purpose of this tutorial the string will be “How old are you?â€:
Code:
cout << "How Old Are You?" << endl;
This is the changed cout << statement.
So let’s get the users input after your cout << statement add this line of code:
Code:
cin >> age;
This line of code makes use of the iostream include and because we are using the standard library namespace we can just write cin. The cin parts asks the user to define what they should define age as e.g. (33). Like the cout statement you need the insertion operator, however for getting user input you use greater than signs rather than less than. After the insertion operator you say what variable you want the user to define and then end with a semicolon.
Add the following line under your cin line:
Code:
cout << "You Are " << age << " Years old!" << endl;
This uses the cout function and the insertion operator as used by our previous cout statement and also a string. However, unlike before you have the variable name surrounded by insertion operators then another string. The insertion operators basically take the value of age and place in the output, note: the insertion wrapped variable must be followed by two Ҡor a string e.g. in this case we used a string. The cout ends with the << endl; like before. The cin.ignore(); will not work here as it will end the program prematurely and having nothing will do the same so instead replace
Code:
cin.ignore(); with system(“PAUSEâ€);
If you know compile and run or debug this program it will ask for the users age, then allow the user to input an age then say how old they are.
Full source code listing:
Code:
#include <iostream>
using namespace std;
int main(){
int age;
cout << "How Old Are You?" << endl;
cin >> age;
cout << "You Are " << age << " Years old!" << endl;
system("PAUSE");
return 0;
}
I hoped this help people in understanding the basics of a C++ program, more lessons on C++ coming soon.
Quote:Who has two thumbs and doesn't give a crap.......Bob Kelso......Have we met?