CSIT341 LECTURE 1

INPUT/OUTPUT :

OUTPUT :

  • Uses ostream class
  • cout is the object of the class ostream
  • Output Operator is  << , the stream insertion operator
  • Can do both formatted and unformatted output

 

statement

output

 

cout<<”Hello word”;

Hello word

 

 

int a=3, b=4;

cout<<”a= “<<a<<” b= “<<b;

a=3         b=4

 

int a=3, b=4;

cout<<”a= “<<a<<”
b= “<<b;

a=3

b=4

“ and are reserved characters

if we want to print these characters to the screen

cout<<""Hello word"";

 

“Hello word”

 

cout<<"Hello word";

 

Hello word

 

cout<<""Hello word"";

 

”Hello word”

 

cout<<""Hello word"";

 

“Hello word”

 

cout<<""Helloword"";

 

“Helloword

INPUT :

  • Uses istream class
  • cin is the object of the class istream
  • cin uses the operator >> which is called extraction operator
  • cin can be used to activate functions get() or getline()

cin can be used to enter data into variables in cascaded form :

            cin>>x1>>x2>>y1

 

The data to be entered into x1, x2 and y1 should be separated by the so called whitespace characters.

White space characters are generated by pressing the space bar, the tab key or the enter key.

The extraction operator skips over whitespace characters. This is okay when reading numeric data. When reading string type data this will create problems. Consider the following example :

 

 

char name[25];

cin >> name ;               if we type in from the keyboard the string “Ali Can”

cout<<name;                output  è Ali      (not Ali Can)

 

To overcome this we will use get() function with cin to read in the string with whitespace characters.

 

get() with parameter.

cin.get(name,25);

cout<<name;  
 

Exercises:      int age; float gpa; char name[20];

Statement

Input data

output

 

cin>>age;

cout<<age

15

 

15

Read int value

cin>>age>>cgpa;

cout<<age<<” ”<<cgpa;

20   3,5

 

20       3,5

Reads int and float value

cin>>name;

cout<<name;

Ali Can Mertcan

 

Ali

Reads name until it reach the white space

cin.get(name,20);

cout<<name;

Ali Can Mertcan

 

Ali Can Mertcan

Reads 20 characters include white space, terminated with enter key

cin.get(name,10);

cout<<name;

Ali Can Mertcan

 

Ali Can M

Reads first 10 characters includes white space

cin.get(name,20, ‘t’);

cout<<name;

Ali Can Mertcan

 

Ali Can Mer

Reads first 20 characters includes white space or reads until reachs to character ‘t’

cin.ignore(5);

cin.getline(name,20);

cout<<"
"<<name;

Ali Can Mertcan

 

an Mertcan

Ignores first 5 characters and reads 20 characters includes white space

cin.ignore(8);

cin.getline(name,20);

cout<<"
"<<name;

Ali Can Mertcan

 

Mertcan

Ignores first 8 characters and reads 20 characters includes white space

get() without parameter.

Reads single character includes white space.

  char gender;

  gender=cin.get();

  cout<<"
"<<gender;

 

char letters;

while((letters=cin.get())!=’A’)

  cout<<letters;                                     enter: ali veli hasAn 

                                                            output: ali veli has

 

char ch; int number=0;                          enter : ali can mertcan 1234

while((ch=cin.get())!=EOF)                  //loop terminated when user input <cntr+z>

{  number++

    cout<<ch;                                         output: ali can mertcan 1234

}

cout<<”
”<<number;                           21

 

EOF Character:

  • EOF is actually a   –1
  • Terminate data from the console enter <ctrl>+z
  • cin returns a 0 (false) when end of data is encountered

Kaynak: sct.emu.edu.tr/csit341
belgesi-1132

Gelen Popüler Aramalar:

Belgeci

Share
Published by
Belgeci

Recent Posts

Basic Grammar-Prepositions

A preposition is a word that connects one thing with another, showing how they are…

2 saat ago

Ağlayan Bir Bebek Nasıl Rahatlatılır?

Bütün bebekler ağlar. Ve iki aylıkken geceleri en fazla iki saat süren > huysuzluk dönemi…

14 saat ago

10 Yaratıcılık Kuralı

1.   En iyi fikri bulmak için bütün fikirleri topla ve kötü olanları içinden çıkar. 2.  …

1 gün ago

PowerPointSlayt Nasil Hazırlanır?

Boş bir slayt görüntüsü cesaret kırıcı olabilir, ama korkmayın. Kısa sürede slaytları nasıl dolduracağınızı öğreneceksiniz.…

2 gün ago

Depresyon Nedir?

   DEPRESYON NEDİR?       İnsanlar zaman zaman kendilerini üzüntülü ve mutsuz hissederler. İşinden ayrılmak, sevdiğini…

2 gün ago

“Burun Düşmesine” Son

Marmara Üniversitesi (MÜ) Tıp Fakültesi Plastik ve Rekonstrüktif Cerrahi Anabilim Dalı Öğretim Üyesi Doç. Dr.…

3 gün ago