0

I am part of a team of three moderate-aptitude programmers, and we are currently attempting to write a program in C++ to embody our new encryption algorithm. However, we have encountered an error which indicates that we are surpassing the length of a string, at some point. I have tried debugging this, myself, but I have had no luck. It's in the jumble() function, though ...

Anyway, here's a paste of our sourcecode with a temporary main() function: http://pastebin.com/GvvYAsKg

It compiles fine, but upon running, we get the following error:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr Aborted

6
  • 2
    Would you please re-indent the code properly and paste it over here? Commented May 3, 2013 at 18:46
  • As almost an aside: what makes you think that writing your own encryption function is a good idea? Commented May 3, 2013 at 18:50
  • Moderate programmers as opposed to militant programmers? Commented May 3, 2013 at 18:53
  • Philip, it's for an engineering class project. Everybody is supposed to design something. Our team decided to do an encryption algorithm. Commented May 3, 2013 at 18:57
  • n.m., I should have said moderately-skilled. We aren't the most experienced programmers, but we're not completely novice, either. Commented May 3, 2013 at 18:58

2 Answers 2

7

One of your problems is in this line:

for(int i = 0; i < ( inPT.length() + 1 ); i++)

This will mean you attempt to access inPT[inPT.length] which is one character beyond the end of the string. You want

for(int i = 0; i < inPT.length(); i++)

or possibly to be using a more idiomatic C++ construct like std::for_each so you can't make this kind of fencepost error. As noted in the comments, running this in a debugger would have pointed this out to you pretty quickly.

Sign up to request clarification or add additional context in comments.

2 Comments

Or the new C++11 range-base for loop: for (char c : inPT) { ... }
This was also causing the problem. Both your answer and John's were correct! Many thanks!
7

Presumably this piece of code

if(modCount >= inPT.length())
{
    modCount = 0;
}
int mod = inKey.at(modCount);         

is meant to read

if(modCount >= inKey.length())
{
    modCount = 0;
}
int mod = inKey.at(modCount);         

Your guard on an out of range access on inKey is using the wrong variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.