我有这个功能。它的目标是首先取出数组的最后一个字符,如果它是字母,则将其大写。如果它是返回键(ASCII 值 10 )或空行,也将其打印出来。所有其他字符,请勿打印。请注意我的 Sentinel_value = 10。除了我的 else 语句之外,它工作正常。它不打印返回键。输出全部在一行上。有什么建议吗?
void EncryptMessage (ofstream& outFile, char charArray[], int length)
{
int index;
int asciiValue;
int asciiValue2;
char upperCased;
char finalChar;
for (index = length-1; index >= 0 ; --index)
{
upperCased = static_cast<char>(toupper(charArray[index]));
if (upperCased >= 'A' && upperCased <= 'Z')
{
asciiValue = static_cast<int>(upperCased) - 10;
finalChar = static_cast<char>(asciiValue);
outFile << finalChar;
}
else
{
asciiValue2 = static_cast<int>(charArray[index]);
if (asciiValue2 == SENTINEL_VALUE)
{
outFile << asciiValue2;
}
}
}
}
最佳答案
asciiValue2
是一个 int
,因此它的 ASCII 值被插入到流中(两个字符,“1”和“0”),而不是它的字符表示形式。将 asciiValue2
声明为 char
就可以了。
关于C++ 加密 ascii 值打印数组中的返回键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10015729/