ใช้สำหรับดึงอักขระจาก Stream เป็น Unformatted Input และเก็บไว้ใน s เป็น C-String จนกว่าจะเจอ Delimiting Character หรือเขียนครบ n อักขระ (รวม Null Terminator) รูปแบบการประกาศคือ:
basic_istream& getline (char_type* s, streamsize n); basic_istream& getline (char_type* s, streamsize n, char_type delim);
พารามิเตอร์:
- s - Pointer ไปยังอาร์เรย์อักขระ ที่เก็บอักขระที่ดึงมาเป็น C-String
- n - จำนวนอักขระสูงสุดที่จะเขียน (รวม Null Terminator)
- delim - อักขระ Delimiter ระบุชัดเจน การดึงอักขระต่อเนื่องจะหยุดทันทีที่อักขระถัดไปเท่ากับค่านี้
ฟังก์ชันคืนค่า Basic_Istream Object (*this)
ตัวอย่าง
#include <iostream>
using namespace std;
int main () {
char name[256], title[256];
cout << "Please, enter your name: ";
cin.getline (name, 256);
cout << "Please, enter your favourite movie: ";
cin.getline (title, 256);
cout << name << "'s favourite movie is " << title;
}
ผลลัพธ์
Please, enter your name: Jack Please, enter your favourite movie: The Boss Baby Jack's favourite movie is The Boss Baby