开发者

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

开发者 https://www.devze.com 2023-04-08 17:23 出处:网络
It's dif开发者_C百科ficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. Fo
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

精彩评论

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

关注公众号