CSIT341 LECTURE 1
INPUT/OUTPUT :
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<<” | 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 :
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<<" | Ali Can Mertcan |
an Mertcan | Ignores first 5 characters and reads 20 characters includes white space |
cin.ignore(8); cin.getline(name,20); cout<<" | 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:
Kaynak: sct.emu.edu.tr/csit341
belgesi-1132
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…
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…
Mustafa Kemal Atatürk 1881 yılında Selânik'te Kocakasım Mahallesi, Islâhhâne Caddesi'ndeki üç katlı pembe evde doğdu.…
Eğer bir insanın başına 'elektroensephalograf' (ezberlemeniz gerekmez!) adını taşıyan bir cihaz bağlarsanız, o insanın yaydığı…