开发者

Is there any way to input a string in C++? [closed]

It's dif开发者_C百科ficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜