Topic : Fast Introduction to C++
Author : Clayman
Page : << Previous 2  
Go to page :


C - How to Program

One of the best books written on C. Great for all levels of programming.

--> Online books

You can find many online netbooks from code.box.sk Thinking in C++ is nearly the best one on C++, a must-read.



C++ TUTORIAL 2: I/O & Variables



TABLE OF CONTENTS

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

2. Console Output (cout)
2.1. Escape character
2.2. Basic calculations

3. Variables & constants
3.1. What is a variable?
3.2. Data types
3.3. int & long
3.4. double
3.5. char
3.6. Constants

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

5. Exercises
5.1. Finding area
5.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 not read the 1st tutorial you can find it from code.box.sk C/C++ section. I advise you 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.




2. CONSOLE OUTPUT (COUT)


2.1. Escape character

To display our name we would have typed:

cout<<"Bill Gates";

but what if we wanted to display:

My name is "Bill"

Have a few tries on this. Because in the cout statement the (") signs indicate the start and end of a string like "this is a string" it would be impossible to display " on the screen. The (\) escape character is used to solve this. So if you ant to display : My name is "Bill" you'd type:

cout<<"My name is \"Bill\""<<

There are special characters which form an escape sequence when used with the escape characters. The character "escapes" from the normal interpretation of characters in a string. For examle the end of line character "n":

cout<<"The first line.\n";
cout<<"The second line";


Note that we did not use endl(ine). Escape sequences include:

SYMBOL EFFECT
------ ------
\n go to next line
\" double quotation mark
\' single quotation mark
\\ single backslash
\t tab (move 8 spaces)
\a alert (with the beep sound)


2.2. Basic calculations

To display numbers using cout you simply type it without enclosing it in quotation marks:

cout<<"A Number: "<<2600<<endl;

Note that each item needs the << operator to its left. Also simple calculations can be performed using cout:

cout<<"Total Money: "<<((5*34)+(5*17))<<endl;

sign description
---- -----------
+ addition
- subtraction
/ division
* multiplication
% modulus division




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 variable will 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 a variable 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 an example 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,767
long integers -2,147,483,647 to +2,147,483,647
char 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: ^+#$~@2
For example:

char iamachar;
iamachar = 'C';
cout<<iamachar<<


displays: C

3.6. Constants

A constant is defined by typing "const" to the left of a variable statement. So its like const-type-identifier-assignment:

const double pi = 3.14159;

The identifier pi will represent the value 3.14159 untill the end of the program. It can not be changed because it is a CONSTANT value. Trying to change the value of a constant results in an error.




4. CONSOLE INPUT (CIN)


4.1. How?

Programs are a great lot useful when the user is able to interact with the program. c(onsole) in(put) pauses a program allowing the user to type a value for the variable assignment. For example:

cin>>counter;

makes the program wait for the user to enter a value and press enter and then assigns that value to the variable counter. cin is the console input stream and it is a part of the iostream library which cout belongs to also. The >> is called 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 <iostream.h>

int main()
{
int rate; // defining of a variable named rate

cout<<"Please rate Jotun by entering a number between 1-5 :";
cin>>rate; // get the rating from user
cout<<"You gave jotun "<<(rate*20)<<" percent";

return(0);
}


First the user is urged to enter a rating between 1 and 5 for the mp3 Jotun and then the value entered by the user is converted to percentage and displayed on the screen.




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 TUTORIAL
If you want to add something or modify the tutorial in any way please e-mail me first [clayman@boxnetwork.net] Feel free to distribute this as you wish.


Page : << Previous 2