Saturday, January 17, 2009

Lesson 2: Input and Output

Level: Beginner

In this lesson we will discuss taking input from the user and giving output to the user in C++. Remember that if you know these basics you can skip them.

We will later be programming using a software called 3D GameStudio. Please click here to download a free trial version of 30 days. Its around 50MB. You have to buy the software if you want to make commercial games with it.

As I have told you in Lesson 1 that you have to include the files 'iostream.h' and 'conio.h' if you have to use input and output in your program.

The command for input is 'cin' followed by '>>' and then followed by the name of the variable you wish to store the input in. 'cin' stands for console in.

The command for output is 'cout' followed by '<<' and then followed by what string you want to print on the screen. 'cout' stands for console out.

Click here to download an example.

In the example, we first include the files 'iostream.h' and 'conio.h'. Then, we start the main function. Then, we introduce characters named as 'name' (command: 'char') followed by []. [] brackets mean that the user can enter infinite amount of characters. If you want to limit the user, you can write some value in it. After that, we give an output to the user to write his/her name, which is stored in value 'name'.

Then, the screen is cleared and output is given.

No comments: