Why doesn't this display the string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Protoman

    #1

    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

  • Neelesh Bodas

    #2
    Re: Why doesn't this display the string?


    Protoman wrote:[color=blue]
    > for(;itEnc!=itE nd;++itEnc,++it Source,++itKey)
    > {
    > if (itKey==itKeyEn d) itKey=key.begin ();
    > string c((*itSource)-((*itKey)-'A'));[/color]

    There is no constructor to create a string from an int. 'c' is a string
    whereas the initializer argument is an integer.
    [color=blue]
    > if (c[itEnc]<'A') c[itEnc]+=26;[/color]

    itEnc is an iterator, not an integer. Hence you need to use *itEnc
    [color=blue]
    > else
    > *itEnc=c;[/color]

    LHS is a char, RHS is a string. There is no valid conversion.
    [color=blue]
    > }
    > return decrypted;
    > }
    >[/color]

    Hope this helps.

    Comment

    • Jay Nabonne

      #3
      Re: Why doesn't this display the string?

      On Thu, 17 Nov 2005 22:47:22 -0800, Protoman wrote:
      [color=blue]
      > 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[/color]

      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

      Comment

      • Thomas J. Gritzan

        #4
        Re: Why doesn't this display the string?

        Protoman schrieb:[color=blue]
        > 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'));[/color]

        Do you mean 'char c'?
        [color=blue]
        > if (c[itEnc]<'A') c[itEnc]+=26;[/color]

        You cant use an iterator as an index. You could write:

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

        But that makes no sense here.
        [color=blue]
        > else
        > *itEnc=c;
        > }
        > return decrypted;
        > }[/color]

        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

        Comment

        • Protoman

          #5
          Re: Why doesn't this display the string?

          Decrypt something!!!!

          Comment

          • Jim Langston

            #6
            Re: Why doesn't this display the string?

            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;
            }


            Comment

            • Howard

              #7
              Re: Why doesn't this display the string?


              "Jim Langston" <tazmaster@rock etmail.com> wrote in message
              news:TRUff.5359 $El5.4030@fe06. lga...[color=blue]
              > Lets see if I can figure out what you're trying to do.[/color]
              [color=blue]
              > // if (c[itEnc]<'A')
              > // c[itEnc]+=26;
              > if (c A')
              > *itEnc = c + 26;
              >[/color]

              if (c A') ???

              -Howard


              Comment

              • Howard

                #8
                Re: Why doesn't this display the string?


                "Protoman" <Protoman2050@g mail.com> wrote in message
                news:1132369566 .294967.174610@ f14g2000cwb.goo glegroups.com.. .[color=blue]
                > Decrypt something!!!!
                >[/color]

                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


                Comment

                • Karl Heinz Buchegger

                  #9
                  Re: Why doesn't this display the string?

                  Howard wrote:[color=blue]
                  >
                  > "Protoman" <Protoman2050@g mail.com> wrote in message
                  > news:1132369566 .294967.174610@ f14g2000cwb.goo glegroups.com.. .[color=green]
                  > > Decrypt something!!!!
                  > >[/color]
                  >
                  > 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.[/color]

                  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
                  kbuchegg@gascad .at

                  Comment

                  • Thomas J. Gritzan

                    #10
                    Re: Why doesn't this display the string?

                    Howard schrieb:[color=blue]
                    > "Protoman" wrote:[color=green]
                    >>Decrypt something!!!!
                    >>[/color]
                    >
                    >
                    > 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?)[/color]

                    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

                    Comment

                    Working...