【问题标题】:C++ encrypting ascii value printing return key from arrayC ++加密ascii值打印数组中的返回键
【发布时间】:2012-04-04 16:50:19
【问题描述】:

我有这个功能。它的目标是取数组的最后一个字符,如果是字母则大写。如果它是返回键(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;
            }   
        }
    }
}

【问题讨论】:

    标签: c++ arrays loops casting ascii


    【解决方案1】:

    asciiValue2int,所以它的 ASCII 值被插入到流中(两个字符,'1' 和 '0'),而不是字符表示。将asciiValue2 声明为char 就可以了。

    【讨论】:

      【解决方案2】:

      ascii 10 只是一个换行符。 EOL 字符因您所在的系统而异
      windows = CR LF
      linux = LF
      osX = CR

      而不是 outfile&lt;&lt;asciiValue2;

      试试 outfile&lt;&lt;endl;

      endl 扩展为您所在系统的 EOL 字符序列。

      【讨论】:

        猜你喜欢
        • 2021-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-23
        • 1970-01-01
        相关资源
        最近更新 更多