#include
#include
using namespace std;
int main(void)
{
char ch;
int i=1;
char *before = new char[1],*temp;
while (cin >> ch)
{
temp = new char[i];
temp[i - 1] = ch;
for (int j = 0; j < i - 1; j++)
temp[j] = before[j];
delete[] before;
before = temp;
++i;
}
for (int j = 0; j < i - 1; j++)
cout << temp[i];
}
最后
for (int j = 0; j < i - 1; j++)
cout << temp[i];
错了吧,循环变量是j,应该是
for (int j = 0; j < i - 1; j++)
cout << temp[j];
而且temp[i]是一个申请的存储空间()
语法上没发现什么问题,逻辑上不知道你想实现的是什么功能,没法检查,不过最后的输出的数组的下标应该是j吧。
temp = new char[i];
i= 1;
这个数组只有一个元素,再++i;数组已经越界了
最后输出的时候temp[i]应该改为temp[j]