user1193717
user1193717

Reputation: 99

C++ encrypting ascii value printing return key from array

I have this function. And its goal is to take the last character first of an array, uppercase the character if it is a letter. If it is a return key (ASCII value 10 ) or blank line, print that out too. All other characters, dont print. Note my sentinel_value = 10. Its working fine except for my else statement. It's not printing the return key. The output is all on one line. Any suggestions?

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

Upvotes: 2

Views: 405

Answers (2)

jrok
jrok

Reputation: 55395

asciiValue2 is an int, so it's ASCII value is inserted in the stream (two characters, '1' and '0'), not it's character representation. Declare asciiValue2 as char and you should be fine.

Upvotes: 1

Charlie
Charlie

Reputation: 1031

ascii 10 is just a line feed. EOL characters differ depending on the system you are on
windows = CR LF
linux = LF
osX = CR

Instead of outfile<<asciiValue2;

try outfile<<endl;

endl expands to the EOL character sequence for the system you are on.

Upvotes: 1

Related Questions