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.
全站熱搜