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

BİOGAZ

İnsanoğlunun çok süratli bir şekilde artan ihtiyaçlarına cevap veren sosyal ve endüstriyel gelişmeler, beraberinde bazı…

10 saat ago

Boşaltım Sistemi

Boşaltım sistemi vücutta homeostazın sağlanmasında çok önemli bir yere sahiptir.Böbrekler, üreterler ve mesaneden oluşan boşaltım…

22 saat ago

Atatürk ve Spor

Büyük Atatürk'ün ölümünü takip eden günlerde, o zamanlar yalnız Avrupa'nın değil, dünyanın en güçlü günlük…

1 gün ago

Atatürk’ün Hayatı

 Mustafa Kemal Atatürk 1881 yılında Selânik'te Kocakasım Mahallesi, Islâhhâne Caddesi'ndeki üç katlı pembe evde doğdu.…

2 gün ago

Özdeşlik

Bir şeyin başka bir şey değil de zorunlu olarak kendisi olması; bir şeyin kendisiyle bir…

2 gün ago

Uyurken Beynimiz Neler Yapıyor ?

Eğer bir insanın başına 'elektroensephalograf' (ezberlemeniz gerekmez!) adını taşıyan bir cihaz bağlarsanız, o insanın yaydığı…

3 gün ago