2012-04-04 37 views
2

我有這個功能。它的目標是將數組中的最後一個字符作爲首字母,如果它是一個字母,則使用大寫字母。如果它是一個返回鍵(ASCII值爲10)或空白行,則將其打印出來。所有其他字符,不打印。注意我的sentinel_value = 10。它的工作正常,除了我的else語句。它不打印返回鍵。輸出全部在一行上。有什麼建議麼?C++加密ascii值打印返回鍵從數組

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; 
      } 
     } 
    } 
} 

回答

1

asciiValue2是一個int,所以它的ASCII值被插入到流中(兩個字符,'1'和'0'),而不是它的字符表示。聲明asciiValue2char,你應該沒問題。

1

ascii 10只是一個換行符。 EOL字符會隨系統你是在
窗口= CR LF
上Linux = LF
OSX版= CR

而不是 outfile<<asciiValue2;

嘗試 outfile<<endl;

ENDL擴展到EOL您所在系統的字符序列。