我怎么能strcat的一个字符数组字符C ++ [英] how can I strcat one character to array character in c++

查看:110
本文介绍了我怎么能strcat的一个字符数组字符C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 cin.get()一个字符,并把它添加到阵列的性格。我用的strcat但单字符有错误。请帮助我,如果你知道。感谢所有的答案。

I want to get one character from cin.get() and add it to a array character. I use strcat but the single character has an error. please help me if you know. thanks for all answers.

void main (void)
{
 char e[80]="hi";
 char c;
 cin.get(c);
 strcat(e,c);
 cout << "e: " << e << endl;
 getch();
}

这就是我想这样做我的code的一部分。

This is part of my code that I want to do this.

推荐答案

stncat()连接两个字符串,方法签名看起来是这样的,

stncat() concatenates two strings, method signature looks like this,

char * strncat ( char * destination, const char * source, size_t num );

但您尝试来连接一个char这是不对的!

but you are trying to concatenate a char which is not right!

由于您使用的是C ++,它是安全的,容易做,在C ++风格,而不是使用C style.So使用

As you are using C++, it is safe and easy to do it in C++ style rather than using C style.So use

std::string cAsStr(c);   // Make the string
e += aAsStr;             // + operator does the concatenation 

如果你是不顾一切做在C风格,使用方法:

If you are desperate to do it in the C style, use:

char cAsStr[] = { c, '\0' }; // Making a C-style string
strcat(e, cAsStr);           // Concatenate

这篇关于我怎么能strcat的一个字符数组字符C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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