Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的字符数组的显示短于最后几个字符。C++;_C++_Arrays_Dynamic - Fatal编程技术网

C++ 我的字符数组的显示短于最后几个字符。C++;

C++ 我的字符数组的显示短于最后几个字符。C++;,c++,arrays,dynamic,C++,Arrays,Dynamic,我想知道为什么字符数组的显示短了几个字符。但是,当我使用长度+2时,将显示所有字符。我不确定我做错了什么。谢谢你的帮助。我正在使用Dev-C++ #include <iostream> #include <string.h> using namespace std; char *Appendstring(char *a, char *b, char *c, char *d, char *e) // will append b to the end of a { /

我想知道为什么字符数组的显示短了几个字符。但是,当我使用长度+2时,将显示所有字符。我不确定我做错了什么。谢谢你的帮助。我正在使用Dev-C++

#include <iostream>
#include <string.h>


using namespace std;

char *Appendstring(char *a, char *b, char *c, char *d, char *e)  // will append b to the end of a
{
// char *buffer = new char[strlen(a)+strlen(b)+1];

 static char buffer[90];

    char *p=buffer;
    while(*p++=*a++); // Copy a into buffer
    while(*p++=*b++); // Copy b into buffer right after a
    while(*p++=*c++);  // Copy c into buffer right after b
    while(*p++=*d++);  // Copy d into buffer right after c
    while(*p++=*e++);  // Copy e into buffer right after d
    *p=0; // Null-terminate the string
    return buffer;  
}

int main ()
{
    char *new_string;
    int length;
    char *str="Because";
    char *add="it has been";
    char *addstr1="very warm";
    char *addstr2="lately";
    char *addstr3="Summer is coming!";


    length=strlen(str)+strlen(add)+strlen(addstr1)+strlen(addstr2)+strlen(addstr3)+1;  //total length of the new string

    new_string=Appendstring(str, add, addstr1, addstr2, addstr3);
    for (int i=0; i<=length+2; i++)  //Why do I need to do length+2 to have all characters displayed???
  cout<<new_string[i];

    return 0;
}
#包括
#包括
使用名称空间std;
char*Appendstring(char*a,char*b,char*c,char*d,char*e)//将b追加到a的末尾
{
//char*buffer=新字符[strlen(a)+strlen(b)+1];
静态字符缓冲区[90];
char*p=缓冲区;
while(*p++=*a++);//将a复制到缓冲区中
while(*p++=*b++);//将b复制到a之后的缓冲区中
while(*p++=*c++);//将c复制到b后面的缓冲区中
while(*p++=*d++);//将d复制到c之后的缓冲区中
while(*p++=*e++);//将e复制到d之后的缓冲区中
*p=0;//Null终止字符串
返回缓冲区;
}
int main()
{
char*新的_字符串;
整数长度;
char*str=“因为”;
char*add=“它已经”;
char*addstr1=“非常温暖”;
char*addstr2=“最近”;
char*addstr3=“夏天来了!”;
length=strlen(str)+strlen(add)+strlen(addstr1)+strlen(addstr2)+strlen(addstr3)+1;//新字符串的总长度
new_string=Appendstring(str、add、addstr1、addstr2、addstr3);

对于(int i=0;i,因为字符串复制代码是错误的。它也复制字符串末尾的空字节

试试这个

while (*a) // Copy a into buffer
    *p++ = *a++;
while (*b) // Copy b into buffer
    *p++ = *b++;

等等。

因为字符串复制代码错误。它也会复制字符串末尾的空字节

试试这个

while (*a) // Copy a into buffer
    *p++ = *a++;
while (*b) // Copy b into buffer
    *p++ = *b++;

等等。

实际上,您正在for循环中打印出4个不可打印的字符

字符串中可打印字符的总数为50。长度变量为51,因为向其中添加了1。然后for循环从0到51+2,这将打印出总共54个字符

但是,由于Appendstring函数错误地嵌入了John描述的空字节,因此字符串中有4个空字符。当您将空字符流式传输到cout时,它是不可打印的,并且不显示任何内容

如John所述,一旦更改了Appendstring函数,就不需要向length添加1,for循环应该是

for (int i=0; i<length; i++)

for(int i=0;i您实际上在for循环中打印出4个不可打印的字符

字符串中可打印字符的总数为50。长度变量为51,因为向其中添加了1。然后for循环从0到51+2,这将打印出总共54个字符

但是,由于Appendstring函数错误地嵌入了John描述的空字节,因此字符串中有4个空字符。当您将空字符流式传输到cout时,它是不可打印的,并且不显示任何内容

如John所述,一旦更改了Appendstring函数,就不需要向length添加1,for循环应该是

for (int i=0; i<length; i++)

<代码> >(int i=0;i + 1),这就是为什么应该使用<代码> STD::String < /C> > C++代替CHAR数组。这是在尝试你的建议之后的输出:“因为它已经是非常热的夏天即将到来!”而我现在的产品的输出是:“因为它最近一直很温暖,夏天是COMI。“@Angew,赋值的要求是使用动态字符数组,并附加它们。我不允许为此赋值使用std::string。@T4000所以在尝试我的建议后,输出是正确的?还是仍然存在问题?@john,这是尝试您的建议后的输出:“因为它已经被发现,warmlatelySummer即将到来!”整个字符串打印出来,除了没有空间,它应该有。+ 1,这就是为什么应该使用<代码> STD::String < /C> > C++代替char数组。这是在尝试你的建议之后的输出:“因为它已经是非常热的夏天即将到来!”而我现在的数据集的输出是:“因为最近天气很暖和,所以夏天是comi“@Angew,作业的要求是使用动态字符数组并附加它们。我不允许在此作业中使用std::string。@T4000所以在尝试我的建议后,输出是正确的?还是仍然有问题?@john,这是尝试您的建议后的输出:”因为它已经被发现了,温暖的夏天就要来了!”整个字符串都被打印出来了,除了它应该有的地方没有空格。