Increment in loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

22nd Aug 2018, 8:01 PM
Mukul
Mukul - avatar
4 Answers
+ 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.
23rd Aug 2018, 2:56 AM
Mooaholic
Mooaholic - avatar
+ 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.
23rd Aug 2018, 1:37 AM
Mukul
Mukul - avatar
+ 1
Thank you Mooaholic
23rd Aug 2018, 7:53 AM
Mukul
Mukul - avatar
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?
22nd Aug 2018, 9:14 PM
Mooaholic
Mooaholic - avatar