我正在尝试编写将 ASCII 转换为十进制、添加并转换回 ASCII 的软件
I'm trying to write software that converts ASCII to decimal, adds to it, and converts back to ASCII
我的代码有问题。这是我到目前为止所拥有的:
#include <iostream>
using namespace std;
int main()
{
char word[128];
int x = 0;
int v;
int shift;
int sv;
cin >> shift;
cin >> word;
while (word[x] != ' ') // While the string isn't at the end...
{
cout << int(word[x]) << " "; // Transform the char to int
x++;
v = int(word[x]);
sv = v + shift;
cout << sv;
}
return 0;
}
这是凯撒密码,至少是其中的一部分。
当我输入时:
shift=1
word=f
我希望结果是:
102 103
相反,我最终得到这个:
102 1
我做错了什么?有更好的方法吗?
移动循环结束时递增x
的行。在显示移位值之前,您正在递增x
。
while (word[x] != ' ') // While the string isn't at the end...
{
cout << int(word[x]) << " "; // Transform the char to int
v = int(word[x]);
sv = v + shift;
cout << sv;
x++;
}
循环计数器通常在完成所有处理后在循环结束时递增。在您的情况下,x
充当循环计数器,因为它正在确定while
循环的条件。
因此,语句x++
应该在所有处理之后出现,即在最后一次cout
之后。
在 for 循环的情况下,
iteration_expression
在循环的每次迭代之后和重新评估条件之前执行。通常,这是递增循环计数器的表达式。
因此,您可以将上述while
更改为for
如下所示:
for(x = 0; word[x] != ' '; x++)
然后,您不必在循环内递增x
。
相关文章:
- 在C++中使用 ASCII 代码将输入从小写转换为大写
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 我正在尝试编写将 ASCII 转换为十进制、添加并转换回 ASCII 的软件
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- 如何将ASCII数字转换为从文件中提取的整数
- 将非 ASCII 字符转换为英语对应字符C++
- 在Qt中将QString转换为ASCII值,反之亦然
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 如何在不将其转换为 ASCII 字符的情况下显示 QByteArray 的十六进制值
- 如何在 c++ 中将字节转换为 ASCII 十六进制表示形式?
- 如何使用 ASCII 转换使用字符堆栈计算后缀表达式
- 将 ASCII 文件转换为 C++ 的二进制格式
- 将字符转换为ASCII返回数字太大
- MFC Cedit将非ASCII字符转换为ASCII
- 使用 c++ 是否可以将 ASCII 字符转换为十六进制?
- 如何将字符串转换为 ASCII 值的总和
- 将句子转换为C 中的ASCII代码
- 在整数中转换ASCII文件列
- 如何将字符数转换为十进制并返回或转换ASCII'A'-'Z'/'a'-'