0

I'm trying to create a caesar cipher in C++, and it keeps infinitely looping. Specifically, when inputting a non-numerical value for the key or for encrypting/decrypting, it loops the reset function infinitely, and accepts an input when the user never gave one. Any ideas as to why this happens?

#include <iostream>
#include <cstring>
using namespace std;

bool x = false;
//Checks if the user wants to continue or not.
bool reset(bool x){
    char cont;
    while(cont != 'n' && cont != 'y'){
        cout << "Would you like to continue? y/n \n";
        cin >> cont;
        if(cont == 'n'){
            x = true;
        }
        if(cont != 'n' && cont != 'y'){
            cout << "Invalid answer. \n";
        }
    }
    return x;
}

//Encoding the text/shifting it up.
string encode(char message[], int key){
    int chAscii;
    for(int i = 0; message[i] != '\0'; i++){
        chAscii = message[i];
        chAscii = abs(chAscii);
        //Encoding for lowercase letters.
        if(chAscii >= 'a' && chAscii <= 'z'){
            chAscii = chAscii + key;
            chAscii = abs(chAscii);
            //If the ASCII value of the character in question
            //is greater than z, loop back around to a.
            if(chAscii > 122){
                chAscii = chAscii - int('z') + int('a') - 1;
            }
            message[i] = chAscii;
        //Encoding for capital letters.
        }else if (chAscii >= 'A' && chAscii <= 'Z'){
            chAscii = chAscii + key;
            //If the ASCII value of the character in question
            //is greater than Z, loop back around to A.
            if(chAscii > 'Z'){
                chAscii = chAscii -'Z' + 'A' - 1;
            }
            message[i] = chAscii;
        }
    }
    return message;
}

//Decoding the text/shifting it down.
string decode(char message[], int key){
    int chAscii;
    for(int i = 0;message[i] != '\0'; i++){
        chAscii = message[i];
        //Encoding for lowercase letters.
        if(chAscii >= 'a' && chAscii <= 'z'){
            chAscii = chAscii - key;
            //If the ASCII value of the character in question
            //is less than than z, loop back around to a.
            if(chAscii < 97){
                chAscii = chAscii + 'z' - 'a' + 1;
            }
            message[i] = chAscii;
        //Encoding for capital letters.
        }else if (chAscii >= 'A' && chAscii <= 'Z'){
            chAscii = chAscii - key;
            //If the ASCII value of the character in question
            //is less than than Z, loop back around to A.
            if(chAscii < 'A'){
                chAscii = chAscii + 'Z' - 'A' + 1;
            }
            message[i] = chAscii;
        }
    }
}

int main() {
    while(x == false){
        //Initializing variables.
        int choice;
        char message[1000];
        int key;
        //Takes user input for what to encode/decode and by how much.
    cout << "Enter text to be coded, ended by '~': ";
    cin.getline(message, 1000, '~');
    cout << "Enter amount to shift: ";
    cin >> key;
    cout << "Enter your choice:\n1 for Encryption, 2 for Decryption\n";
    cin >> choice;
    //Checks to see if we are encoding or decoding the text
    if(choice == 1){
        cout << encode(message, key);
    }else if(choice == 2){
        cout << decode(message, key);
    }else{ 
        cout << "Invalid choice.";
    }
        x=reset(x);
    }
}
  • 1
    `char cont; while(cont != 'n' && cont != 'y'){` everything from this point on is undefined behavior, because this code is accessing an uninitialized variable. That's it. – Sam Varshavchik Oct 04 '20 at 00:58
  • Given what @SamVarshavchik pointed out, you could have seen the problem by doing very simple debugging. You should have been able to print out the values, and noted that the `cont` value used in the loop was strange. Showing no debugging effort is one reason for a downvote. – PaulMcKenzie Oct 04 '20 at 01:02
  • @PaulMcKenzie: at least he provided a MWE, don't be so draconian, or you'll discourage people from posting – Giogre Oct 04 '20 at 01:10
  • @Lingo -- [I downvoted because...](https://idownvotedbecau.se/nodebugging/). What I am saying is not draconian. – PaulMcKenzie Oct 04 '20 at 01:11
  • 1
    @Lingo "_at least he provided a MWE_" Technically, it isn't even [mre], since it isn't as minimal as necessary to reproduce the problem. – Algirdas Preidžius Oct 04 '20 at 01:18
  • @PaulMcKenzie I apologize for not trying that earlier, however after giving cont a value of 'a' and outputting it, it simply ignores the cin line and outputs 'a', as if its getting input from something other than the user. Also, this issue only occurs when giving a non-numerical input when asked for the key and to encrypt(user input of 1) or decrypt(user input of 2). when it gets to that while loop normally, there are no issues. – JoeMcSteve Oct 04 '20 at 01:18
  • whatever, but there is much worse round ... – Giogre Oct 04 '20 at 01:20
  • Since you determined that the error is in (or around) the `reset` function, you could have removed all mention of ciphers from your question. If you eliminate everything not needed to test `reset` from your main function, it might look like `int main() { while(x == false){ /* Omitted processing */ x=reset(x); } }`. If you can reproduce the error with just that, there is a lot of code that could be stripped from your example to make it much more minimal. Even if you need to add a line to `main` that reads from `cin`, you would still not need any cipher processing. – JaMiT Oct 04 '20 at 02:09
  • Creating a [mre] is a skill that improves with practice. With more experience, you might find that you can reduce your example down to something like [Infinite loop with cin when typing string while a number is expected](https://stackoverflow.com/questions/5864540/infinite-loop-with-cin-when-typing-string-while-a-number-is-expected). – JaMiT Oct 04 '20 at 02:19
  • @pppery yes it did. using this article and a couple others I was able to completely solve my issues, thank you. – JoeMcSteve Oct 04 '20 at 03:18

0 Answers0