Is there any way to input a string in C++? [closed]
Is there any alternative way to input a string (along with blank spaces) without using std::string
in C++?
I know, std::string
is the standard way. But, I am just curious.
std::istream::getline(char *, streamsize)
Stolen shamelessly from http://www.cplusplus.com/reference/iostream/istream/getline/
// 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;
}
By using std::string
. There is no reason not to use it.
If, by some obscure chance, you have additional requirements why you cannot use std::string
, please state those requirements as they will impact the code.
Update: If the input is newline delimited, use string s; std::getline(cin, s);
You can use getline
- the old C way of doing things. But why would you want to do this?
Ref: http://www.crasseux.com/books/ctutorial/getline.html
Or if you like headaches you could even use system calls!
精彩评论