473,804 Members | 3,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why doesn't this display the string?

Why doesn't this display the string?

string decrypt(const string& ciphertext,cons t string& key)
{
string decrypted;
decrypted.resiz e(ciphertext.si ze()); // allocates the string for the
correct size.
string::iterato r itEnc=decrypted .begin(),itEnd= decrypted.end() ;
string::const_i terator itSource=cipher text.begin();
string::const_i terator itKey=key.begin (),itKeyEnd=key .end();
for(;itEnc!=itE nd;++itEnc,++it Source,++itKey)
{
if (itKey==itKeyEn d) 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 1552

Protoman wrote:
for(;itEnc!=itE nd;++itEnc,++it Source,++itKey)
{
if (itKey==itKeyEn d) 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,cons t string& key)
{
string decrypted;
decrypted.resiz e(ciphertext.si ze()); // allocates the string for the
correct size.
string::iterato r itEnc=decrypted .begin(),itEnd= decrypted.end() ;
string::const_i terator itSource=cipher text.begin();
string::const_i terator itKey=key.begin (),itKeyEnd=key .end();
for(;itEnc!=itE nd;++itEnc,++it Source,++itKey)
{
if (itKey==itKeyEn d) 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,cons t string& key)
{
string decrypted;
decrypted.resiz e(ciphertext.si ze()); // allocates the string for the
correct size.
string::iterato r itEnc=decrypted .begin(),itEnd= decrypted.end() ;
string::const_i terator itSource=cipher text.begin();
string::const_i terator itKey=key.begin (),itKeyEnd=key .end();
for(;itEnc!=itE nd;++itEnc,++it Source,++itKey)
{
if (itKey==itKeyEn d) 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,cons t string& key)
{
string decrypted;
decrypted.resiz e(ciphertext.si ze()); // allocates the string for the
correct size.
string::iterato r itEnc=decrypted .begin(), itEnd=decrypted .end();
string::const_i terator itSource = ciphertext.begi n();
string::const_i terator itKey = key.begin(),itK eyEnd = key.end();
for(;itEnc != itEnd; ++itEnc, ++itSource, ++itKey)
{
if (itKey==itKeyEn d)
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,cons t string& key)
{
string decrypted;
decrypted.resiz e(ciphertext.si ze()); // allocates the string for the
correct size.
string::iterato r itEnc=decrypted .begin(), itEnd=decrypted .end();
string::const_i terator itSource = ciphertext.begi n();
string::const_i terator itKey = key.begin(),itK eyEnd = key.end();
for(;itEnc != itEnd; ++itEnc, ++itSource, ++itKey)
{
if (itKey==itKeyEn d)
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*******@rock etmail.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**********@g mail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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**********@g mail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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
2132
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 working when I include data entry fields to the form: I click on Delete or Edit inside of DataGrid and get this error: "Error: Object doesn't support this property or method" If I remove data entry fields from the form - DataGrid allows to...
4
5108
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 if I do not enter anything in the form in IE, then form won't be submitted but in other browsers it is submitted. I checked that JavaScript is enabled in Netscape, Opera, Mozilla, Firefox,
16
4931
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
15
3080
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" onChange="this.form.submit();"> it doesn't work... <input type="hidden" name="hiddenopt" value="secret"> <select name="list" onChange="document.forms.submit();">
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.