Ask coding questions

← Back to all posts
Trouble with class functions, c++
Giothecoder (13)

DIFFERENT PROBLEM, SAME TYPE.

EDIT FOR DIFF PROBLEM -
I am just having trouble getting the functions of encrypt and decrypt to work
at first if I just set the string (stream) to the return object with a = it would work
for the encrypt function but not the decrypt function. Now neither are working!
I suggest if you are just now looking at this post to still read the background info
I made an encryption class containing a key and a group of cipher alphabets with two functions: one to encrypt and one to decrypt.

I input my cipher alphabet seed thing I input my key, every thing is going fine, and then, right after I input my message that I’m going to encrypt, my program just crashes with a return of -1 I think it was.
I did some tests and I am relatively certain that my encrypt and decrypt functions are making the program crash.

Here’s the link to the code...

https://repl.it/@Giothecoder/ForkedScientificPup

The cipher alphabet is a polyalphabetic cipher which is basically a series of monoalphabetic ciphers alphabets.
A monoalphabetic cipher being something like
Q = A, W = B,E = C,etc.

When using a polyalphabetic cipher one must also use a key. The key simply tells you which cipher alphabet to use from the group of monoalphabetic ciphers
I First input a monoalphabetic cipher, which I use to make a polyalphabetic cipher by shifting it over for each one in the group, and then I input a key, which tells me how to encrypt the message using the polyalphabetic cipher. Lastly I input the message that I then encrypt, letter by letter.

If you have ABSOLUTELY ANY questions about what I have said, comment. If you have an answer to my question please for gs sake comment I’m dying here.
Thanks :)

Commentshotnewtop
SPQR (405)

the std::bad_alloc can be solved by commenting out std::cin >> a; on line 68.

Giothecoder (13)

@SPQR thanks but its still just giving me the same error as before, same place even

mwilki7 (277)

I just cleaned up some input code and initalized some variables for you.
I'm not sure if the algorithm works but the program finishes now:
https://repl.it/@mwilki7/ForkedScientificPup

Giothecoder (13)

@mwilki7 Thanks! I totally missed that, I put in the variable initializing, but this kept my way of input mostly because I’m told all the time not to mix ways to input stuff and I need the getline function for later when I make it so I can take in more than a word and not have to worry about spaces. Now Though my message string (which is now p) is not being changed by my encrypt function.

UPDATE -
the encrypt function is working, but only because I forced stream to
be equal to the return object (like you did) of my encrypt function,
but my decrypt function will not work even when I tried the same
method
on it.