C ++:Char *和cin [英] C++: Char* and cin

查看:300
本文介绍了C ++:Char *和cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用cin输入一个不确定长度的字符串到char *变量;

I would like to input a string of indefinite length to a char * variable using cin;

我可以这样做:

char * tmp = "My string";
cout << tmp << endl;
system("pause");

它工作得很好。

但我无法做到这一点:

 char * tmp
 cin >> tmp;

你能给我一个铰链什么问题吗

Could you give me a hing what's wrong"

推荐答案

好吧,你没有为 char * 创建一个对象。

Well, you havn't created an object for the char* to point to.

char* tmp = new char[MAX_LENGTH];


另一种方法是:

should make it work better (you have to define MAX_LENGTH). Another way to do this is:

std::string strtmp;
cin >> strtmp;
const char* tmp = strtmp.c_str();

此方法将意味着您不需要使用 new

This method would mean that you need not use new.

这篇关于C ++:Char *和cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆