Multiple substrings = Error

P

Pistolen08

Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?
Thanks,
-Elliot
 
R

red floyd

Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?


First, post a minimal, COMPILABLE example that exhibits the behavior in
question.

Second, VC6 is horrendously old, out of date, and non-Standard
compiliant. Upgrade to VC2005 (Express version available for free as in
beer), or to GCC (available free as in speech and beer).

Third, you don't need to do the "= string()" on the flipStringN
variables. They will be default constructed unless you provide some
other sort of initializer.
 
V

Victor Bazarov

Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?

FAQ 5.8. Also, VC++ v6 has a very good debugger. Use it.

V
 
J

John Harrison

Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?
Thanks,
-Elliot

All the code you posted is fine. The problem is somewhere in the code
you didn't post. It usually is, which is why it is recommended to post
complete programs.

john
 
P

Pistolen08

I can not post the entire code, but here is some more.

string str = string();
string binString = string();
string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();
string toReverse = string();
out << toEncrypt;
binString = out.str();

flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);

binString = flipString4 + flipString3 + flipString2 + flipString1;

cout << "after flip " << binString << endl;
 
V

Victor Bazarov

I can not post the entire code, but here is some more.

string str = string();
string binString = string();
string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

As suggested before, you can safely drop all those '= string()'
parts. Strings are correctly initialised without that.
string toReverse = string();

This doesn't seem relevant at all.
out << toEncrypt;

What's the content of 'toEncrypt'?
binString = out.str();

Add

assert(binString.size() >= 8);

here
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);

binString = flipString4 + flipString3 + flipString2 + flipString1;

cout << "after flip " << binString << endl;

If you don't want to post your actual code, it's fine with us,
but you have to realize that we're not mind readers.

If I have to guess, the error is in your 'toEncrypt' output. It
most likely does not produce long enough a string. Check the length
of 'binString'.

As a freebee, here is what your fragment should look like:

out << toEncrypt;
string binString = out.str();

string flipString1 = binString.substr(0,2);
string flipString2 = binString.substr(2,2);
string flipString3 = binString.substr(4,2);
string flipString4 = binString.substr(6,2);

string binString = flipString4 + flipString3
+ flipString2 + flipString1;

cout << "after flip " << binString << endl;

As you hopefully see, there is *absolutely* no need to define those
variables before they are going to be given some value.

V
 
P

Pistolen08

As suggested before, you can safely drop all those '= string()'
parts. Strings are correctly initialised without that.


This doesn't seem relevant at all.


What's the content of 'toEncrypt'?


Add

assert(binString.size() >= 8);

here






If you don't want to post your actual code, it's fine with us,
but you have to realize that we're not mind readers.

If I have to guess, theerroris in your 'toEncrypt' output. It
most likely does not produce long enough a string. Check the length
of 'binString'.

As a freebee, here is what your fragment should look like:

out << toEncrypt;
string binString = out.str();

string flipString1 = binString.substr(0,2);
string flipString2 = binString.substr(2,2);
string flipString3 = binString.substr(4,2);
string flipString4 = binString.substr(6,2);

string binString = flipString4 + flipString3
+ flipString2 + flipString1;

cout << "after flip " << binString << endl;

As you hopefully see, there is *absolutely* no need to define those
variables before they are going to be given some value.

V



Thanks for your help. I will address your questions in their order.

I understand that I don't need the = string(). It is just a habit,
and I will remove them.

string toReverse = string(); is a temporary string that I made in
order to do another string manipulation to. I have not posted the
code that uses that variable, so I can understand your confusion.

toEncrypt is the input string (8 binary characters) which is passed to
the encrypt() function.

what is the purpose of "assert(binString.size() >= 8);"? It does
correctly read the string using the way I coded it.

I can not post the entire code, since if I did, the encryption would
be pointless since someone could just look here. I thank you for
understanding that.

toEncrypt is a global variable which stores the unencrypted string of
binary. I am copying it into binString so that I am not directly
modifying toEncrypt. Both toEncrypt and binString have a length of 8
chars through each pass of the function. After the string is
encrypted, binString is returned to the calling function where it can
be written to a file.

Thanks for all your help.
 
T

Thomas Tutone

[snip]
If you don't want to post your actual code, it's fine with us,
but you have to realize that we're not mind readers.
[snip]


I can not post the entire code, since if I did, the encryption would
be pointless since someone could just look here. I thank you for
understanding that.

No one really wants you to post all the code. What everyone wants you
to do is provide the shortest piece of _compilable_ code that
reproduces the problem you are having. This requires you to do some
work - you need to reduce your long, complicated, possibly proprietary
program to something nice and short that illustrates the problem,
_but_ _that_ _we_ _can_ _compile_. Then we can cut and paste your
code, compile it, and see whether we have the same problem you do.
Granted, it takes some work on your part to reduce the code in this
manner, but for that effort, you get two major benefits:

1. If you actually post that short, complete, code example, you will
have someone actually figure out your problem, rather than playing
this current guessing game. Why? Because usually the problem is in
part of the code you didn't post before.

2. Most times, in the process of shortening the code to something you
can post in full, you figure out the problem on your own - typically
in an "aha" moment where you say to yourself, "I can't believe I
missed that."

Anyway, just wanted to explain why people keep saying "post actual
code."

Good luck.

Best regards,

Tom
 
R

rossum

toEncrypt is the input string (8 binary characters) which is passed to
the encrypt() function.

what is the purpose of "assert(binString.size() >= 8);"? It does
correctly read the string using the way I coded it.
The assert is to check that the string returned from toEncrypt() is
indeed eight or more characters long. If, as seems probable, there is
a fault in toEncrypt() which causes it to return a string less than
eight characters long then this assert will catch it and halt your
program.
I can not post the entire code, since if I did, the encryption would
be pointless since someone could just look here. I thank you for
understanding that.
This is probably a mistake. Either your encryption is reliant on the
secrecy of the algorithm or you have hard coded the encryption key
into your source code. Both of these are mistakes and should be
avoided.

A secure encryption algorithm can be publicly known (like DES or AES)
and still be secure as long as the key is kept secret. If the key is
hard coded into the source code then the key cannot be secure and the
system is compromised.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,362
Latest member
OrderTrimKetoBoost

Latest Threads

Top