void decryption() //this function decrypts whatever the user inputs using keys 1 - 100, providing all possible decryptions
{
     const int MAX = 100;
     string code, output;
     char array[100];
     
     cout << "Enter what you would like decrypted" << endl;
     cin >> code; //herein lies the problem
     strcpy(array, code.c_str());
          
     
  
     for (int key = 0; key <= 100; key++)
     {
         for (int i = 0; i <= strlen(code.c_str()); i++)
         {
             if (array[i] - key < 32) 
                 output += ((array[i] - key) + 127) - 32;
             else
                 output += (array[i] - key);
         }
         cout << "Output using key " << key << ": " << output << endl;
         output.erase();
     }
     getchar();
     getchar();
     exit(1);
     
}

This is my code so far, it all works fine except that the cin >> code takes in the '\n' newline character when I dont want it to.
I've tried using
cin.ignore(1000, '\n');
getline (cin, code);
to get around the problem but it does the same thing as the cin >> code;

What can I do to fix this?

Thank you.

Recommended Answers

All 2 Replies

No, cin >> does not take in the newline.
Your problem is the processing loop

for (int i = 0; i <= strlen(code.c_str()); i++)

Running your loop to <= string length means you are processing the NULL terminator at the end of the string.
Loops for handling strings should be written as

for (int i = 0; i < strlen(code.c_str()); i++)

And, you don't need to use the strlen and c_str with the string type, it has its own size or length methods, like code.length()

Thank you very much, that fixed my problem!

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.