-3

As I am new to coding c++ and am taking a object oriented class, I need some help. For this code I want to encrypt it by shifting all of the text that is enter by 1 ascii digit i.e. a -> b, b-> etc. I am suppose to use all ascii values 32 - 126 but I cant figure out why when i try to encrypt anything I only get a "^" as a output.

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

void encrypt (string &encrypt)
{
    string encryption;

    for (int i = 0; i < encrypt.length(); i++)
    {
        encrypt[i]++;
        if (i > 126){
            encrypt  = i - 94;
        }
        else if (i < 32){
            encrypt = i + 94;
       }
    }
}

void decrypt (string decrypt)
{
    string decryption;

    for ( int i = 0; i < decryption.length(); i ++)
    {
        decryption[i]--;
        if (i > 126){
            decrypt  = i + 94;
        }
        else if (i < 32){
            decrypt = i - 94;
        }
    }
}

int main ()
{
    string option;
    string encryption1;
    string decryption1;
    cout << "Do you want to encrypt or decrypt? \n";
    cin >>  option;

    if (option == "encrypt")
    {
        cout << "What do you want to encrypt \n";
        cin.ignore();
        cin.clear();

        getline (cin, encryption1);
        encrypt ( encryption1);
        cout << encryption1 << " " << endl;
    }

    if (option == "decrypt")
    {
        cout << "What do you want to decrypt \n";
        cin.ignore();
        cin.clear();

        getline (cin, decryption1);
        encrypt ( decryption1);
        cout << decryption1 << " " << endl;
    }
    return 0;**
zaph
  • 111,848
  • 21
  • 189
  • 228
jonnyboy12
  • 11
  • 1
  • 4
  • Have you run this through a debugger? Stick a breakpoint in each function and make sure they're doing what you expect. – MrEricSir Sep 26 '16 at 20:05
  • I have tried to run it through gdb and i can see it go through once but as soon as it goes through twice it only reads the first letter and makes it to ^ no matter what i type in – jonnyboy12 Sep 26 '16 at 20:06
  • `encrypt = i - 94;` should not compile and if it does it is not what you want. – NathanOliver Sep 26 '16 at 20:06
  • @nathanoliver It compiles fine but displaying is the issue. Any insight on how i should fix this? – jonnyboy12 Sep 26 '16 at 20:08
  • Usually "help me debug" questions are better suited to asking a TA or professor in person, if you have one. SO is better for questions that can help more than one person. – Gillespie Sep 26 '16 at 20:08
  • 3
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Sep 26 '16 at 20:08
  • Proper indentation also helps. Fixed. – zaph Sep 26 '16 at 20:14
  • 2
    Think about this: `if (i > 126)` – David Schwartz Sep 26 '16 at 20:15
  • 1
    Likewise think about your decryption for-loop condition: `i < decryption.length()` . Um... `decryption` is a local variable declared and never assigned anything. So it's `length()` will be **zero**, which means that loop is about to go nowhere. No sense in sugar coating it, so I won't bother. Most of this code looks like an outright *guess* at attempting to solve the problem stated. Get a [good book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?s=1|13.3313) and spend your time studying how the language and its library work before attempting this. – WhozCraig Sep 26 '16 at 20:30
  • you are not referenceing values in the array and instead setting the whole string in your if statements. i.e. encrypt = i - 94 should be encrypt[i] = i - 94 – Jacob McCarthy Sep 26 '16 at 20:30

1 Answers1

0

I did something similar to this in my level one programming class last year. We created a Vigenere Cipher that is based off of the architecture of the Cesar cipher. Something that is useful is to first create a 0 base, i.e. if you are working with values a through b, subtract a from each of the characters that you are encoding first, do the math, double check that the value falls within the range of values (32 through 126 == 0 through 94) and then return the char + a. This would mean rewriting your logic and function to take a char instead of the entire string at once. As for why you are getting '^' as your only output, the ACSII code for '^' is 94 and your code:

for ( int i = 0; i < decryption.length(); i ++)
    {
        decryption[i]--;
        if (i > 126){
            decrypt  = i + 94;
        }
        else if (i < 32){
            decrypt = i - 94;
        }
    }
}

is setting the entire string to char 94. You set i = 0 then say that if i is less than 32 then the string decrypt is equal to the char 0 + 94 which is equal to 94 which is equal to '^'

DRoc101
  • 63
  • 8