0

Building a simple program that multiplies the ASCII value of chars in a string by 3 to encrypt and then divide by 3 to decrypt. So far I got the encryption part down but whenever I enter what the encryption gave and try to decrypt it doesn't work. I think it has something to do with the buffer stream but I could be wrong if anyone could help.

#include<iostream>
using namespace std;

int main()
{
string message;
int charValue;
int counter;
int encrypt;
char choice;
char quit = 'N';



while (quit == 'N')
{
    cout << "Enter E to encrypt or D to Decrypt\n";
    cin >> choice;
    toupper(choice);

    cout << "Enter text no spaces: ";
    cin >> message;

    int messagelen = message.length();
    string stringArray[255];

    if (choice == 'E')
    {
        for (counter = 0; counter < messagelen; counter++) //*3 to ascii val
        {
            stringArray[counter] = message[counter] * 3;

        }
        for (counter = 0; counter < messagelen; counter++)
        {
            cout << stringArray[counter];
        }
    }
    else
    {
        for (counter = 0; counter < messagelen; counter++) // divide 3 to ascii val
        {
            stringArray[counter] = message[counter] / 3;

        }
        for (counter = 0; counter < messagelen; counter++)
        {
            cout << stringArray[counter];
        }
    }
    cout << "\nY to go again N to quit";
    cin >> quit;

}
return 0;
}
Leo
  • 51
  • 6
  • `string stringArray[255];` this creates an array of 255 _strings_. That doesn't really make sense, since you're accepting just one string on input. More likely what you need is an array of `int`s (`short` or `unsigned short` would be plenty big enough too). Store `message[counter] * 3` at each appropriate index of that array, then you can divide by 3 and cast to `char` to retrieve the ASCII value. – yano Oct 07 '21 at 05:13
  • Given that the length of the string is unknown at compile time, using `std::vector` might make more sense. – Chris Oct 07 '21 at 05:15
  • Also worth noting that your while loop runs while `quit == 'N'` but you prompt the user to input `'N'` to quit. – Chris Oct 07 '21 at 05:18

2 Answers2

0

It would genuinely help you to break this down into smaller problems. Let's "encrypt" a std::string into a std::vector<int>:

std::vector<int> encrypt_msg(std::string s) {
    std::vector<int> v;

    for (auto ch = s.begin(); ch != s.end(); ch++) {
        v.push_back(static_cast<int>(*ch) * 3);
    }

    return v;
}

Then let's "decrypt" a message, performing the transformation in reverse.

std::string decrypt_msg(std::vector<int> v) {
    std::string s;

    for (auto i : v) {
        s += static_cast<char>(i / 3);
    }

    return s;
}

Now you can test and see that your individual functions that do one thing work, and putting together the overall program should be much easier.

Chris
  • 26,361
  • 5
  • 21
  • 42
0

This is a working implementation, although I agree with the other answer that you should use encrypt and decrypt functions. I found quite a few other bugs with your code working through it. You should enable all warnings with -Wall -Werror and fix them:

#include <iostream>
#include <vector>
#include <sstream>

int main()
{
  // removed some unused variables
  std::string message;
  size_t counter;
  char choice;
  char quit;
  // use vector of int instead of array of strings
  std::vector<int> encryptArray;

  // change to do while loop. Not particularly necessary, but I think
  // it makes more sense in this case. Your condition is broken. If the
  // user enters 'Y' at the end to go again, then the quit == 'N'
  // condition is false and the program terminates.
  do
  {
    std::cout << "Enter E to encrypt or D to Decrypt\n";
    std::cin >> choice;
    // toupper returns a value, you need to assign it to choice.
    // Not capturing the return value makes this a noop.
    choice = toupper(choice);

    if (choice == 'E')
    {
      std::cout << "Enter text no spaces: ";
      std::cin >> message;

      size_t messagelen = message.length();
      // initialize vector to input message length size
      encryptArray = std::vector<int>(messagelen);

      for (counter = 0; counter < messagelen; counter++) //*3 to ascii val
      {
        encryptArray[counter] = message[counter] * 3;
      }

      // Note, this 2nd loop is more work than you need, you could
      // simply put the std::cout line in the loop above below the
      // assignment
      for (counter = 0; counter < messagelen; counter++)
      {
        // added the separator just for clarity. You could also print
        // hex bytes
        std::cout << encryptArray[counter] << "|";
      }
    }
    else
    {
      // all the data we care about is in the vector now
      for (counter = 0; counter < encryptArray.size(); counter++) // divide 3 to ascii val
      {
        // you don't want to /3 what's in the message here, you want
        // to /3 the encrypted values, which are in the vector
        encryptArray[counter] = encryptArray[counter] / 3;
      }
      // plenty of ways to convert the vector to a string, this is not
      // a "modern" way.
      // Note, you could skip this loop entirely, and in the one
      // above, simply do ss << (char)(encryptArray[i] / 3);
      // Not necessary to write the data back to encryptArray first.
      std::stringstream ss;
      for (size_t i=0; i<encryptArray.size(); ++i)
      {
        ss << (char)encryptArray[i];
      }
      std::cout << "decrypted string: " << ss.str() << std::endl;
    }

    std::cout << "\nY to go again N to quit: ";
    std::cin >> quit;
  } while(quit != 'N'); // loop until quit == N

  return 0;
}

Finally, I removed using namespace std;, here's why

Things get squirrely working with stdin on godbolt, but here's a working demonstration, at least initially.

yano
  • 4,827
  • 2
  • 23
  • 35