c++tutorial2

Embed Size (px)

Citation preview

  • 7/24/2019 c++tutorial2

    1/5

    \ /-------- ** ** ** /-- --------\ / \/------- * * * * * --- --------\/ /\------- * * * * * \-- --------/\

    / \-------- ** ** ** . BOX.SK --------/ \______________________________________________________________________-[ Written by: Clayman ]-[Mail: [email protected]]--[June-14.2000]-[ver : 1.0]----------------------------------------------------------------------

    C++ TUTORIAL 2: I/O & Variables

    ****You can distribute, make copies or do anything else you wish with this tutorial but if you want to change anything ask me first.****----------------------------------------------------------------------

    TABLE OF CONTENTS

    1. Introduction1.1. What's this tutorial about?1.2. What do I need before starting?

    2. Console Output (cout)2.1. Escape character2.2. Basic calculations

    3. Variables & constants3.1. What is a variable?3.2. Data types3.3. int & long3.4. double3.5. char3.6. Constants

    4. Console Input (cin)4.1. How?4.2. Example program

    5. Exercises

    5.1. Finding area5.2. Einstein's formula

    ---------------------------------------------------------

    1.INTRODUCTION

    1.1. What's this tutorial about?

    In this tutorial - which is absolutely a beginners tutorial - you will learn how to obtain values from the user and use them. You don't need much konwledge of C++.

    1.2. What do I need before starting?

    Reading the first tutorial "C++ TUTORIAL 1: INTRODUCTION TO C++" is recommended, also you need a C++ compiler, a working head, some time. If you did notread the 1st tutorial you can find it from code.box.sk C/C++ section. I adviseyou not to copy&paste the code you see here but write it yourself because it will make you learn a lot faster by theaching you the syntax.

    ---------------------------------------------------------

  • 7/24/2019 c++tutorial2

    2/5

    2. CONSOLE OUTPUT (COUT)

    2.1. Escape character

    To display our name we would have typed:

    cout

  • 7/24/2019 c++tutorial2

    3/5

    3. VARIABLES

    3.1. What is a variable?

    Definition: A variable is a named memory location which stores a value.Not clear? Variables can be thought as empty containers. They can be filled by the user or the programmer. To use a variable it must be defined by giving its type and identifier. The type of a variable shows what kind of value the variablewill store. Different data types will be discussed later in part 3.2. The identifier of a variable is the name of the variable. To clear up things look at the example:

    int counter;

    defines a variable with the name of "counter" which stores data of type"int" (integer). Variables are used to hold values. You can assign a value to avariable like this:

    counter = 35;

    The equal o sign (=) represents assignment, and the effect is to store the value 35 in the memory location which goes by the name "counter". There is anexample program in 4.2. illustrating the use of variables.

    3.2. Data types

    A programmer can write his own Data Types but C++ provides several built-in data types. It is important to use the correct data type. The buit-in C++ types are:

    TYPE DESCRIPTION RANGE---- ----------- -----double real numbers 1/(10*308) to 10*308 (+ or -)int integers -32,767 to +32,767long integers -2,147,483,647 to +2,147,483,647char characters all typable & displayable characters

    Now a closer look to each one:

    3.3. int & long

    The int & long data types are used to store pozitive and negative integers. The only difference between int and long is the range of values they can hold. If a decimal is stored in an integer variable the value is truncated: the decimal portion is not stored.

    3.4. double

    The double data type is used to store positive or negative real numbers.This type is also known as floating point. When displaying very large or small

    numbers the computer uses the scientific notation.

    3.5. char

    The char data type stores a single character. Characters include any kind of displayable characters including digits, alphabet &symbols: ^+#$~@2For example:

    char iamachar;

  • 7/24/2019 c++tutorial2

    4/5

    iamachar = 'C';cout iscalled the "get from operator" or the "extraction operator" is used to to get "data" from the input stream.

    4.2. Example program

    Here is an example program illustrating the use of variables and cin:

    /* Downloaded from code.box.sk Mp3 rating program */

    #include

    int main(){

    int rate; // defining of a variable named rate

    coutrate; // get the rating from usercout

  • 7/24/2019 c++tutorial2

    5/5

    5. EXERCISES

    5.1. Finding area

    Make a program which finds the area of a circle by using the radius value entered by the user. Check the syntax if the program does not work.

    5.2. Einstein's formula

    Use the formula e = mcc to find how much energy can be produced using certain amount of user-defined mass.

    ---------------------------------------------------------

    Thanks to:

    -->Cube, to provide such valuable information in the boxnetwork sites.-->all guys at all message boards, for asking tutorials.

    Ke Bea(!?) to:-->all friends in the Koch Part-time Prison.-->Yalcin, for being so lazy.

    ---------------------------------------------------------******

    END OF TUTORIALIf you want to add something or modify the tutorial in any way please e-mail mefirst [[email protected]] Feel free to distribute this as you wish.---------------------------------------------------------******