如何将 cin 转换为 const char*

发布于 2024-11-09 02:17:05 字数 240 浏览 0 评论 0原文

在我的程序中,我通过 iostream 获取输入:

char input[29];
cin >> input;

我需要使用此输入作为此类的参数,该类将此参数作为其构造函数

class::class(const char* value) {
  /* etc */ }

关于如何转换它有任何想法吗?

谢谢

In my program I get input via iostream:

char input[29];
cin >> input;

I need to use this input a parameter for this class that has this parameter as its constructor

class::class(const char* value) {
  /* etc */ }

Any idea on how to convert it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

貪欢 2024-11-16 02:17:05

您应该能够将 input 作为参数传递给构造函数。 char[] 将衰减为 char *,它与 const char * 兼容。

但是: 流式传输到固定长度缓冲区是一个非常糟糕的主意(如果有人提供的输入长度超过 28 个字符怎么办?)。使用 std::string 代替(如 @George 的答案)。

You should just be able to pass input as the argument to your constructor. A char[] will decay to a char *, which is compatible with a const char *.

However: Streaming into a fixed-length buffer is a really bad idea (what if someone provides an input that is more than 28 characters long?). Use a std::string instead (as in @George's answer).

荒岛晴空 2024-11-16 02:17:05
string tmp;
cin >> tmp;
foo(tmp.c_str());
string tmp;
cin >> tmp;
foo(tmp.c_str());
鹿港巷口少年归 2024-11-16 02:17:05

没有办法>>运算符知道它只能读取 29 个字节。
因此,您必须明确指定它:

char input[29] = { 0 }; // note sets all characters to '\0' thus the read will be '\0' terminated.
cin.read(input, 28);    // leave 1 byte for '\0'

或者您可以使用 std 字符串。

std::string word;
cin >> word;  // reads one space seporated word.

Class objet(word.c_str()); // Or alternatively make you class work with strings.
                           // Which would be the correct and better choice.

如果您需要读取整行而不是

std::string line;
std::getline(std::cin, line);

Class objet(line.c_str()); // Or alternatively make you class work with strings.
                           // Which would be the correct and better choice.

上述所有内容中的一个单词 Note,您应该在读取后检查流的状态以确保读取有效。

std::string word;
if (cin >> word)   // using the if automatically checks the state. (see other questions).
{
    Class objet(word);
}

There is no way for the >> operator to know that it can only read 29 bytes.
So you must specify it explicitly:

char input[29] = { 0 }; // note sets all characters to '\0' thus the read will be '\0' terminated.
cin.read(input, 28);    // leave 1 byte for '\0'

Alternatively you can use a std string.

std::string word;
cin >> word;  // reads one space seporated word.

Class objet(word.c_str()); // Or alternatively make you class work with strings.
                           // Which would be the correct and better choice.

If you need to read a whole line rather than a word

std::string line;
std::getline(std::cin, line);

Class objet(line.c_str()); // Or alternatively make you class work with strings.
                           // Which would be the correct and better choice.

Note in all the above you should really check the state of the stream after the read to make sure the read worked.

std::string word;
if (cin >> word)   // using the if automatically checks the state. (see other questions).
{
    Class objet(word);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文