close

We can read a line by using getline function.

There are two situation , we know the size of a line , or 

we don't know the size of a line.

 

if we know the size , 

// istream getline 
#include <iostream> 
using namespace std; 
int main () 
char name[256], title[256]; 
cout << "Enter your name: "
cin.getline (name,256); 
cout << "Enter your favourite movie: "
cin.getline (title,256); 
cout << name << "'s favourite movie is " << title; 
return 0; 
}

ref : http://www.cplusplus.com/reference/iostream/istream/getline/

 

if we don't know the size ,

// getline with strings 
#include <iostream> 
#include <string> 
using namespace std; 
int main () 
string str; 
cout << "Please enter full name: "
getline (cin,str); 
cout << "Thank you, " << str << ".\n"
}
// the getline is a global function in the string library here.

ref : http://www.cplusplus.com/reference/string/getline/

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()