数组输出中缺少字符
Character is missing from the output of the array
我正在尝试使用指针和字符反转此字符串,但我得到的输出缺少第 2 个字符,找不到原因。 就像下面的情况一样,我缺少 B 这个词。
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int size = 100;
char oldText[size];
char newText[size];
char *pntr;
pntr = oldText;
cout << "Write the Text: n";
for (int i=0; i<size; i++)
{
cin.get(pntr,size);
newText[i]=*pntr;
cout << *pntr;
pntr++;
}
cout << "The text backwards is: n";
for (int i = size; i>=0; i--)
{
pntr--;
cout <<newText[i];
}
cout <<endl;
return 0;
}
结果供您参考
此处的代码C++无效。
int size = 100;
char oldText[size];
char newText[size];
您正在使用的 VLA 不是无效的C++代码。阅读此答案以获取更多信息 https://stackoverflow.com/a/54674671/7185790。
相反,你可以用constexpr
来限定int size
,那就没问题了。
constexpr int size = 10;
请参阅以下不使用字符指针的示例:https://rextester.com/AEULY24804
请参阅以下使用字符指针的示例:https://rextester.com/FNPW17676
相关文章:
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 字符的输入/输出操作
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- C++输出奇怪的字符而不是数字 (Windows)
- 如何存储 unicode 字符并将其输出到文件?
- C++中附加的字符*的奇怪输出
- 来自无符号字符缓冲区的虚拟输出
- 将未签名的字符写入输出文件
- 在大小为 10 的 char 数组中输入超过 10 个字符的字符串并输出时,它会显示整个数组而不是前 10 个数组.为什
- 如何在C++中正确地将我的语言字符输出到HTML文件中
- x86 上的指针大小和奇怪的字符输出
- 如何修复C++中的随机字符输出?
- Visual Studio-怪异的字符输出
- 需要我当前的字符输出,这是它在C++中的整数值
- 宽字符输出结果
- 将字符输出随机化为文本文件
- C++和printf-奇怪的字符输出
- 显示无符号字符*和有符号字符*输出的结果是否完全相同
- 下一个回文 .奇怪的字符输出
- 不要将宽字符输出到命令提示符