473,231 Members | 1,907 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,231 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 1525

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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.