By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,564 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,564 IT Pros & Developers. It's quick & easy.

Why doesn't this display the string?

P: n/a
Why doesn't this display the string?

string decrypt(const string& ciphertext,const string& key)
{
string decrypted;
decrypted.resize(ciphertext.size()); // allocates the string for the
correct size.
string::iterator itEnc=decrypted.begin(),itEnd=decrypted.end();
string::const_iterator itSource=ciphertext.begin();
string::const_iterator itKey=key.begin(),itKeyEnd=key.end();
for(;itEnc!=itEnd;++itEnc,++itSource,++itKey)
{
if (itKey==itKeyEnd) itKey=key.begin();
string c((*itSource)-((*itKey)-'A'));
if (c[itEnc]<'A') c[itEnc]+=26;
else
*itEnc=c;
}
return decrypted;
}

Any suggestions? Thanks

Nov 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Protoman wrote:
for(;itEnc!=itEnd;++itEnc,++itSource,++itKey)
{
if (itKey==itKeyEnd) itKey=key.begin();
string c((*itSource)-((*itKey)-'A'));
There is no constructor to create a string from an int. 'c' is a string
whereas the initializer argument is an integer.
if (c[itEnc]<'A') c[itEnc]+=26;
itEnc is an iterator, not an integer. Hence you need to use *itEnc
else
*itEnc=c;
LHS is a char, RHS is a string. There is no valid conversion.
}
return decrypted;
}


Hope this helps.

Nov 22 '05 #2

P: n/a
On Thu, 17 Nov 2005 22:47:22 -0800, Protoman wrote:
Why doesn't this display the string?

string decrypt(const string& ciphertext,const string& key)
{
string decrypted;
decrypted.resize(ciphertext.size()); // allocates the string for the
correct size.
string::iterator itEnc=decrypted.begin(),itEnd=decrypted.end();
string::const_iterator itSource=ciphertext.begin();
string::const_iterator itKey=key.begin(),itKeyEnd=key.end();
for(;itEnc!=itEnd;++itEnc,++itSource,++itKey)
{
if (itKey==itKeyEnd) itKey=key.begin();
string c((*itSource)-((*itKey)-'A'));
if (c[itEnc]<'A') c[itEnc]+=26;
else
*itEnc=c;
}
return decrypted;
}

Any suggestions? Thanks


There is no code in this function to display a string. It returns the
string as a return value. You will need to send it to cout or some other
output function to display it.

- Jay
Nov 22 '05 #3

P: n/a
Protoman schrieb:
string decrypt(const string& ciphertext,const string& key)
{
string decrypted;
decrypted.resize(ciphertext.size()); // allocates the string for the
correct size.
string::iterator itEnc=decrypted.begin(),itEnd=decrypted.end();
string::const_iterator itSource=ciphertext.begin();
string::const_iterator itKey=key.begin(),itKeyEnd=key.end();
for(;itEnc!=itEnd;++itEnc,++itSource,++itKey)
{
if (itKey==itKeyEnd) itKey=key.begin();
string c((*itSource)-((*itKey)-'A'));
Do you mean 'char c'?
if (c[itEnc]<'A') c[itEnc]+=26;
You cant use an iterator as an index. You could write:

if (c[*itEnc]<'A') c[*itEnc]+=26;

But that makes no sense here.
else
*itEnc=c;
}
return decrypted;
}


The identifiers imply that you want to decrypt something here. But it
seems that this code does not decrypt anything. What do you want to do?

Thomas
Nov 22 '05 #4

P: n/a
Decrypt something!!!!

Nov 22 '05 #5

P: n/a
Lets see if I can figure out what you're trying to do.

string decrypt(const string& ciphertext,const string& key)
{
string decrypted;
decrypted.resize(ciphertext.size()); // allocates the string for the
correct size.
string::iterator itEnc=decrypted.begin(), itEnd=decrypted.end();
string::const_iterator itSource = ciphertext.begin();
string::const_iterator itKey = key.begin(),itKeyEnd = key.end();
for(;itEnc != itEnd; ++itEnc, ++itSource, ++itKey)
{
if (itKey==itKeyEnd)
itKey=key.begin();
string c((*itSource)-((*itKey)-'A'));
if (c[itEnc]<'A')
c[itEnc]+=26;
else
*itEnc=c;
}
return decrypted;
}

Okay, you pass in the encrytped text and a key. You want to manipulate each
character in the encrypted text by the key, setting a decrypted string to
the value. Okay.... a few problems. Lets see if this is what you want.

string decrypt(const string& ciphertext,const string& key)
{
string decrypted;
decrypted.resize(ciphertext.size()); // allocates the string for the
correct size.
string::iterator itEnc=decrypted.begin(), itEnd=decrypted.end();
string::const_iterator itSource = ciphertext.begin();
string::const_iterator itKey = key.begin(),itKeyEnd = key.end();
for(;itEnc != itEnd; ++itEnc, ++itSource, ++itKey)
{
if (itKey==itKeyEnd)
itKey=key.begin();

// string c((*itSource)-((*itKey)-'A'));
// Looks like you're trying to subtract from the encrypted character the key
character and 'A'.
char c = (*itSource)-((*itKey)-'A');

// if (c[itEnc]<'A')
// c[itEnc]+=26;
if (c A')
*itEnc = c + 26;

else
*itEnc=c;
//

}
return decrypted;
}
Nov 22 '05 #6

P: n/a

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:TR*****************@fe06.lga...
Lets see if I can figure out what you're trying to do. // if (c[itEnc]<'A')
// c[itEnc]+=26;
if (c A')
*itEnc = c + 26;


if (c A') ???

-Howard
Nov 23 '05 #7

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Decrypt something!!!!


And what... we're supposed to guess, or figure out from incorrect code,
exactly HOW you want to do the decryption?

I believe the question being asked was: what is the algorithm you're trying
to implement? What are those lines of code supposed to do? (And I would
add: how was it encrypted in the first place?)

If the compiles, then try using your debugger, and see exactly what is going
on at each step. If it doesn't compile, then either fix it where the
compiler says there's an error, or show us the code and the error message,
and we can help you find the error. Just saying "this doesn't work" is of
no use to anyone trying to help you.

-Howard
Nov 23 '05 #8

P: n/a
Howard wrote:

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Decrypt something!!!!


And what... we're supposed to guess, or figure out from incorrect code,
exactly HOW you want to do the decryption?

I believe the question being asked was: what is the algorithm you're trying
to implement? What are those lines of code supposed to do? (And I would
add: how was it encrypted in the first place?)

If the compiles, then try using your debugger, and see exactly what is going
on at each step.


And if I may add:
don't debug with feeding a crytped version of the bible to your program
but test with eg.
let it encrypt the a string consisting of a single letter, eg 'B'.
Then step through your decrypter and figure out what and why your
program does to turn that crypted character back to 'B' and why
it doesn't do the right thing.

Yes: this is programming.
First you think about your problem to get an idea on how
to solve it. Then you search for ways to turn that ideas into
program code. Then you test your program and figure out that
it doesn't work the way you expect it to be. Next you need to
figure out where in the first 2 steps you made an error: Did you
translate the idea into wrong code or is there something
in the idea you have missed? From then on, things go
in cycles: fix the program, fix the idea flaw (if it is
fixable). Sometimes this also means: Throw everyting away
and start afresh.

That's you life as beeing a programmer.

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 24 '05 #9

P: n/a
Howard schrieb:
"Protoman" wrote:
Decrypt something!!!!

And what... we're supposed to guess, or figure out from incorrect code,
exactly HOW you want to do the decryption?

I believe the question being asked was: what is the algorithm you're trying
to implement? What are those lines of code supposed to do? (And I would
add: how was it encrypted in the first place?)


Actually I wondered why the hell the OP used such a low secure algorithm
not really worth calling it "encryption". But I didn't want to write
that. :-)

The algorithm is nearly a caesar cipher
(http://en.wikipedia.org/wiki/Caesar_cipher), rotating the alphabet
right by <x> (instead of a constant) where <x> is indicated by the key.

The problem I see is that the OP does not really understand the
differences between char, char* and std::string, and the purpose of
iterators.

Thomas
Nov 25 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.