Increment in loops | SoloLearn: Learn to code for FREE!

+1

Increment in loops

In my code, https://code.sololearn.com/clbZtK23azsn/?ref=app if(key >= 0 && key < 26) { for(int i = 0 ; i < mess.length() ; i++) { mess[i] = encrypt(mess[i], key); cout << key << endl; //key++; } If I have key++ in this loop, The code becomes wrong. I am not able to understand what is the problem.

8/22/2018 8:01:11 PM

Mukul

4 Answers

New Answer

+1

Mukul o ok i understand what you are talking about and your problem is a getline error. you use getline after you have used a normal cin>> doing this the getline is seeing the \n newline and puting that as mess[0] making c in code mess[1] and by that time if you use key++ c is using key 2 instead of 1. to fix this you just have to put cin.ignore(); right before your getline and that will clear out the newline and fix the problem with key++. hope this helps.

+1

The output is shifted wrong using key++. For example, if I have to encrypt the word code by 1 shift without key++, the word would be dpef. But when I put key++ It should be dqgi but it is erhj.

+1

Thank you Mooaholic

0

i dont know much about Caesar Cipher myself but your code looks like it works correctly, and still runs if you put the key++. what is it doing wrong?