Trying to replace '\n' or its decimal equivalent of 10 after it has a decimal value added with its original value of '\n' or 10.

This way the newline character won't appear in the ciphertext as some other visible character.

while (!plainText.eof()){
    
        string plainLine = "";
        getline(plainText, plainLine);
        int lineLength = plainLine.length(); 
        
        for (int i=0; i<=lineLength; i++){
            
            int decimalValue[lineLength];
            char cipherLine[lineLength];
        
            decimalValue[i] = (int)plainLine[i] + shiftValue; 
            cipherLine[i] = (char)decimalValue[i];
            
              if (plainLine[i] == '\n')
                  cipherLine[i] = '\n'; 
                
            cipherText << cipherLine[i];    
        }   
        
        cipherText << '\n';
    }

Recommended Answers

All 6 Replies

if (plainLine[i] == '\n')
                  cipherLine[i] = '\n';

If your getline method is separating based on \n anyway, would you need this statement at all? I think you're doing the right thing in reinserting \n in the ofstream

Thank you for your reply, but can anyone shed some more light on this problem. I just want to make sure the '\n' character doesn't appear in my cipher text as some other character. I want it to be in its normal state so it can't be noticed.

An example of my problem:

¬Å½ÃÀź¿ÀžwÆÅw¸wºÉÀËÀº¸Ãw½ÀÉÊËw˼Ê˃wª¼Å¸Ë¼w›¼ÄƺɸËÊwºÃÆʼ»wɸÅÂÊw«¿ÌÉÊ»¸Ðw¹¼¿ÀÅ»w{‹‡w¹ÀÃÃÀÆÅwÀÅwÇÆÃÀËÀº¸ÃÃÐwÉÀÊÂÐw¤¼»Àº¸É¼wºÌËÊw¸ËwË¿¼w¿¼¸ÉËwƽw¿¼¸ÃË¿wº¸É¼wü¾ÀÊøËÀÆŃw˿θÉËÀžw¸w©¼Ç̹ÃÀº¸Åw¸Ë˼ÄÇËwËÆw»ÆÆÄw§É¼ÊÀ»¼ÅËw™¸É¸ºÂw¦¹¸Ä¸~ÊwÊμ¼ÇÀžwÆͼɿ¸ÌÃ…W
W <<< NEWLINE>>>
«¿¼w¹À»w¹ÐwË¿¼w¹ÀÃÃ~ÊwºÉÀËÀºÊwËÆwɼͼÉʼwºÌËÊwËÆwË¿¼wÇÆÇÌøÉw¤¼»Àº¸É¼wÇÉƾɸÄw½¸Àü»wÆÅw¸wÍÆ˼wƽwŒ„‹‰ƒw»É¸ÎÀžwË¿¼wÊÌÇÇÆÉËwƽwËÎÆw›¼ÄƺɸËÀºw»¼½¼ºËÆÉÊ…w˜ÇÇÉÆ͸ÃwÎÆÌûw¿¸Í¼wÊËÉÀÇǼ»wÆÌËwÄÆżÐwż¼»¼»wËÆwǸÐw½ÆÉw¼ÏǸŻÀžwºÆͼɸ¾¼wËÆw˼ÅÊwƽwÄÀÃÃÀÆÅÊwƽwÌÅÀÅÊÌɼ»w˜Ä¼ÉÀº¸ÅÊ…W
W<<<NEWLINE>>>
«¿¼w¹ÉƸ»¼Éwü¾ÀÊøËÀÆÅw¸ÀÄÊwËÆw¼Ï˼Żw¿¼¸ÃË¿wºÆͼɸ¾¼wËÆwŠˆwÄÀÃÃÀÆÅwοÆwÅÆÎwøºÂwÀ˃wοÀüw¹¸ÉÉÀžwÀÅÊÌɸź¼wÀÅ»ÌÊËÉÐwÇɸºËÀº¼ÊwÊ̺¿w¸Êw»¼ÅÐÀžwºÆͼɸ¾¼wÆÅwË¿¼w¹¸ÊÀÊwƽwÇɼ„¼ÏÀÊËÀžwļ»Àº¸ÃwºÆÅ»ÀËÀÆÅÊ…w«¿Æ̾¿wË¿¼wÆͼɿ¸ÌÃwÀÊw¼ÊËÀĸ˼»wËÆwºÆÊËw¸¹ÆÌËw{ˆwËÉÀÃÃÀÆÅwÆͼÉw¸w»¼º¸»¼ƒwË¿¼wšÆžɼÊÊÀÆŸÃw™Ì»¾¼Ëw¦½½Àº¼w¿¸ÊwʸÀ»wÀËwÎÆÌûwºÌËw½¼»¼É¸Ãw»¼½ÀºÀËÊw¹Ðw{ˆŠ‡w¹ÀÃÃÀÆÅwÆͼÉwË¿¸ËwǼÉÀÆ»ƒw¸Å»wÇÉƹ¸¹ÃÐwɼ»Ìº¼wË¿¼Äw½ÌÉË¿¼ÉwÀÅwË¿¼wˆ‡wм¸ÉÊw¹¼ÐÆÅ»wË¿¸Ë…W

As jonsca said, any newline char in the file opened by the stream called plainText will be eliminated from the stream by getline() as it is the default delimiting char for getline() and getline() doesn't include the terminating char in the string that it uses. That means that plainLine will never have a newline char in it.

Unfortunately I make sense of your example. If you want the cipher version to include the newline char in the encrypted version then you will have to add it back to the string variable extracted from the file by getline() someplace. If you want the newline char to be encrypted like any other char in the string then place it back in the string before encryption or encrypt it separately. If you want to view the encrytped version with the same line breaks as the nonencryted version, then add newline char to the string after it has been encrypted and before you send it to an output device.

As jonsca said, any newline char in the file opened by the stream called plainText will be eliminated from the stream by getline() as it is the default delimiting char for getline() and getline() doesn't include the terminating char in the string that it uses. That means that plainLine will never have a newline char in it.

Unfortunately I make sense of your example. If you want the cipher version to include the newline char in the encrypted version then you will have to add it back to the string variable extracted from the file by getline() someplace. If you want the newline char to be encrypted like any other char in the string then place it back in the string before encryption or encrypt it separately. If you want to view the encrytped version with the same line breaks as the nonencryted version, then add newline char to the string after it has been encrypted and before you send it to an output device.

Thanks everyone. With everyone's help, I realized this is the correct line:

cipherLine[lineLength] = '\n';

This works but I originally thought that when you do .length() that the size returned does not account for the '\n', but I guess it does because I am able to catch it at the length value that is returned.

while (!plainText.eof()){
    
        string plainLine = "";
        getline(plainText, plainLine);
        int lineLength = plainLine.length(); 
        
        for (int i=0; i<=lineLength; i++){
            
            int decimalValue[lineLength];
            char cipherLine[lineLength];
        
            decimalValue[i] = (int)plainLine[i] + shiftValue; 
            cipherLine[i] = (char)decimalValue[i];
            
            cipherLine[lineLength] = '\n';   
                
            cipherText << cipherLine[i];    
        }     
    }

I'm glad you've been able to work out a solution to the problem you encountered, though I don't think it is because of the reason you think. Look at my code below to find out how to prove or disprove whether plainLine will contain a new line char.

string plainLine = "";
int lineLength;
int decimalValue;
int shiftvalue = //some value;
string cipherLine = "";
while(getline(plainText, plainLine))
{    
     lineLength = plainLine.length(); 
     //could display lineLength here to prove plainLine doesn't contain a new line char, assuming you know how many char there is per line not including the new line char by some other means

     //ecrypt char in plainLine
     for (int i = 0; i < lineLength;  i++)
     {
          //another way to visualize if plainLine has a new line char in it
          if(plainLine[i] == '\n')
             cout << "new line char found in planLine" << endl;

          decimalValue = (int)plainLine[i] + shiftValue; 
          cipherLine += (char)decimalValue;
      }  
      //add a new line char to cipherLine since there will be no new line char in plainLine
      cipherLine += '\n'; 
      //write cipherLine to file
      cipherText << cipherLine;
      //start over with empty cipherline
      cipherLine = "";   
   }     
}

I'm glad you've been able to work out a solution to the problem you encountered, though I don't think it is because of the reason you think. Look at my code below to find out how to prove or disprove whether plainLine will contain a new line char.

string plainLine = "";
int lineLength;
int decimalValue;
int shiftvalue = //some value;
string cipherLine = "";
while(getline(plainText, plainLine))
{    
     lineLength = plainLine.length(); 
     //could display lineLength here to prove plainLine doesn't contain a new line char, assuming you know how many char there is per line not including the new line char by some other means

     //ecrypt char in plainLine
     for (int i = 0; i < lineLength;  i++)
     {
          //another way to visualize if plainLine has a new line char in it
          if(plainLine[i] == '\n')
             cout << "new line char found in planLine" << endl;

          decimalValue = (int)plainLine[i] + shiftValue; 
          cipherLine += (char)decimalValue;
      }  
      //add a new line char to cipherLine since there will be no new line char in plainLine
      cipherLine += '\n';
      //write cipherLine to file
      cipherText << cipherLine;
      //start over with empty cipherline
      cipherLine = "";   
   }     
}

So the only newlines are the ones being read from the plainText file itself...

Thank you for your example. Very kind of you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.