473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Why doesn't this display the string?

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
9 1530

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
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
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
Decrypt something!!!!

Nov 22 '05 #5
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

"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops...
4
by: Joe | last post by:
Hello, I have created a login page using dotnet. I am using requiredFieldValidator and noticed that the code works fine in IE but not in Netscape, Opera, Mozilla, Firefox, etc. For example...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
15
by: colyn7 | last post by:
I really can't see what's wrong in my code... the submit() onChange doesn't work.. I've tried.. <select name="ddlTestCenter" id="ddlTestCenter" style="width:180px"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.