Thread: should code snippets work ?

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    should code snippets work ?

    should code snippets work ? especially for those trying to learn cpp ? i downloaded this from dreaming in c

    Code:
    /* cppcaesar.cpp */
    
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
            int difference;
            string msg;
            int crypted;
            int real_difference;
            int i=0;
    
    
            cout<<"Enter the offset : ";
            cin>>difference;
            cin.get();
            if (difference > 26) //Brings back the number if above 26
            {
                    real_difference = difference % 26;
            }
    
            else
            real_difference = difference;
    
            cout<<"Enter the message you want to crypt : ";
            getline(cin, msg);
    
    
                    while (i<= msg.length()) //Converts each letter
                    {
    
                            if(msg[i] == 32) // Detect if the letter is a space
                            {
                                    crypted = msg[i];
                                    cout<<(char)crypted;
                                    i++;
                            }
    
    
                            if ((msg[i] + real_difference) > 'z') //detect if the crypted letter is above 'z'
                            {
    
                                    crypted = (msg[i] + real_difference)-'z'+'a';
                            }
    
                            else
                            {
                                    crypted = msg[i] + real_difference;
                            }
    
                            cout<<(char)crypted;
                            i++;
    
                            if(i>= msg.length()) //If the string is finished
                            break;
                    }
    
    
    
    
    
    
    
            system("PAUSE");
            return 0;
    
    }
    it would not compile untill i changed the headers and added using namespace std;
    even after that the program does not work.

    run
    C:\Dev-Cpp\project333 misc>cppceasor0006
    Enter the offset : 9
    Enter the message you want to crypt : up in uppertown xyz is reading abd math


    ey rw eyynbdxgw hij rc bnjmrwp jkm vjdq

    Press any key to continue . . .

    C:\Dev-Cpp\project333 misc>cppceasor0006
    Enter the offset : -9
    Enter the message you want to crypt : ey rw eyynbdxgw hij rc bnjmrwp jkm vjdq


    \p in \ppeY[o^n _`a iZ Yeading abd ma[h

    Press any key to continue . . .

    C:\Dev-Cpp\project333 misc>
    here are the changes i made that compiles.
    Code:
    /* cppcaesar.cpp */
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
            int difference;
            string msg;
            int crypted;
            int real_difference;
            int i=0;
    
    
            cout<<"Enter the offset : ";
            cin>>difference;
            cin.get();
            if (difference > 26) //Brings back the number if above 26
            {
                    real_difference = difference % 26;
            }
    
            else
            real_difference = difference;
    
            cout<<"Enter the message you want to crypt : ";
            getline(cin, msg);
            cout<<"\n"<<endl;
    
                    while (i<= msg.length()) //Converts each letter
                    {
    
                            if(msg[i] == 32) // Detect if the letter is a space
                            {
                                    crypted = msg[i];
                                    cout<<(char)crypted;
                                    i++;
                            }
    
    
                            if ((msg[i] + real_difference) > 'z') //detect if the crypted letter is above 'z'
                            {
    
                                    crypted = (msg[i] + real_difference)-'z'+'a';
                            }
    
                            else
                            {
                                    crypted = msg[i] + real_difference;
                            }
    
                            cout<<(char)crypted;
                            i++;
    
                            if(i>= msg.length()) //If the string is finished
                            break;
                    }
    
    
    
    
    
            cout<<"\n"<<endl;
    
            system("PAUSE");
            return 0;
    
    }
    is there a prob with the meow modulus 26 method ?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your compile changes look fine, except it should be <string>, not <cstring>.

    As to the code, you should try abcdefghijklmnopqrstuvwxyz with an offset of 9 and see what happens. That should give you a clue what is wrong with your handling of whether the letter is higher than 'z'.

    Then try abcdefghijklmnopqrstuvwxyz with an offset of -9. That should help you figure out what else is missing from the code.

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Your compile changes look fine, except it should be <string>, not <cstring>.
    oops thank you.

    i will try that.

    i changed the headers to the cpp type before putting in "using namespace std;" and it gave errors so i put that in. that brings up a question. i thought that the compiler would default to the standard library. it compiled fine after i added that "using namespace std;". why did it no default to the standard library ?

    what else is missing from the code.
    working on that.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i thought that the compiler would default to the standard library.
    Nope. You have to specify that you want to automatically look in the standard library namespace for the names you use. Many people (including me) prefer to prefix standard library names with std:: instead of using the using directive to avoid name clashes. If the compiler automatically used the std namespace, you wouldn't get that benefit (or that choice).

    >> working on that.
    Good. Your example won't work without it since you need it to work both ways.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just a nit but it's something that looks odd to me (though it also looks like it would still work as is)...
    Code:
    while (i<= msg.length()) //Converts each letter
    {
        ...
    
        i++;
    
        if(i>= msg.length()) //If the string is finished
            break;
    }
    Don't know why the first test is <= with all that extra logic towards the end of the loop there. Why not just:
    Code:
    while (i< msg.length()) //Converts each letter
    {
        ...
    
        i++;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    debug code
    Code:
    /* cppcaesar.cpp */
    
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    int main()
    {
            int difference;
            string msg;
            int crypted;
            int real_difference;
            int i=0;
    
    
            cout<<"Enter the offset : ";
            cin>>difference;
            cin.get();
            if ( difference >  26 ) //Brings back the number if above 26
            {
                    real_difference = difference % 26;
                    cout<<"inside modulus loop";
            }
    
            else
            cout<<"outside loop";
            real_difference = difference;
    
            cout<<"Enter the message you want to crypt : ";
            getline(cin, msg);
            cout<<"\n"<<endl;
    
                    while (i<= msg.length()) //Converts each letter
                    {
    
                       cout<<"message length = "<< msg.length() <<"i = " <<i<<endl;
                            if(msg[i] == 32) // Detect if the letter is a space
                            {
                               cout<<"msg[i] = "<< msg[i]<< " "<<endl;
                                    crypted = msg[i];
                                    cout<<(char)crypted;
                                    i++;
                            }
    
    
                            if ((msg[i] + real_difference) > 'z') //detect if the crypted letter is above 'z'
                            {
                                cout<<" >z msg[i] + real_difference = "<< msg[i] + real_difference <<" "<<endl;
                                    crypted = (msg[i] + real_difference)-'z'+'a';
                            }
    
                            else
                            {
                                 cout<<"3else msg[i] + real_difference = "<< msg[i] + real_difference<< " "<<endl;
                                    crypted = msg[i] + real_difference;
                            }
    
                            cout<<(char)crypted <<" default ";
                            i++;
    
                            if(i>= msg.length()) //If the string is finished
                            break;
                    }
    
    
    
    
    
            cout<<"\n"<<endl;
    
            system("PAUSE");
            return 0;
    
    }
    output
    C:\Dev-Cpp\project333 misc>cppceasor0011
    Enter the offset : 9
    outside loopEnter the message you want to crypt : abcdefghikllmnopqrstuvwxyz


    message length = 26i = 0
    3else msg[i] + real_difference = 106
    j default message length = 26i = 1
    3else msg[i] + real_difference = 107
    k default message length = 26i = 2
    3else msg[i] + real_difference = 108
    l default message length = 26i = 3
    3else msg[i] + real_difference = 109
    m default message length = 26i = 4
    3else msg[i] + real_difference = 110
    n default message length = 26i = 5
    3else msg[i] + real_difference = 111
    o default message length = 26i = 6
    3else msg[i] + real_difference = 112
    p default message length = 26i = 7
    3else msg[i] + real_difference = 113
    q default message length = 26i = 8
    3else msg[i] + real_difference = 114
    r default message length = 26i = 9
    3else msg[i] + real_difference = 116
    t default message length = 26i = 10
    3else msg[i] + real_difference = 117
    u default message length = 26i = 11
    3else msg[i] + real_difference = 117
    u default message length = 26i = 12
    3else msg[i] + real_difference = 118
    v default message length = 26i = 13
    3else msg[i] + real_difference = 119
    w default message length = 26i = 14
    3else msg[i] + real_difference = 120
    x default message length = 26i = 15
    3else msg[i] + real_difference = 121
    y default message length = 26i = 16
    3else msg[i] + real_difference = 122
    z default message length = 26i = 17
    >z msg[i] + real_difference = 123
    b default message length = 26i = 18
    >z msg[i] + real_difference = 124
    c default message length = 26i = 19
    >z msg[i] + real_difference = 125
    d default message length = 26i = 20
    >z msg[i] + real_difference = 126
    e default message length = 26i = 21
    >z msg[i] + real_difference = 127
    f default message length = 26i = 22
    >z msg[i] + real_difference = 128
    g default message length = 26i = 23
    >z msg[i] + real_difference = 129
    h default message length = 26i = 24
    >z msg[i] + real_difference = 130
    i default message length = 26i = 25
    >z msg[i] + real_difference = 131
    j default

    Press any key to continue . . .

    C:\Dev-Cpp\project333 misc>

    C:\Dev-Cpp\project333 misc>cppceasor0011
    Enter the offset : -9
    outside loopEnter the message you want to crypt : jklmnopqrstuvwxyzbcdefghij


    message length = 26i = 0
    3else msg[i] + real_difference = 97
    a default message length = 26i = 1
    3else msg[i] + real_difference = 98
    b default message length = 26i = 2
    3else msg[i] + real_difference = 99
    c default message length = 26i = 3
    3else msg[i] + real_difference = 100
    d default message length = 26i = 4
    3else msg[i] + real_difference = 101
    e default message length = 26i = 5
    3else msg[i] + real_difference = 102
    f default message length = 26i = 6
    3else msg[i] + real_difference = 103
    g default message length = 26i = 7
    3else msg[i] + real_difference = 104
    h default message length = 26i = 8
    3else msg[i] + real_difference = 105
    i default message length = 26i = 9
    3else msg[i] + real_difference = 106
    j default message length = 26i = 10
    3else msg[i] + real_difference = 107
    k default message length = 26i = 11
    3else msg[i] + real_difference = 108
    l default message length = 26i = 12
    3else msg[i] + real_difference = 109
    m default message length = 26i = 13
    3else msg[i] + real_difference = 110
    n default message length = 26i = 14
    3else msg[i] + real_difference = 111
    o default message length = 26i = 15
    3else msg[i] + real_difference = 112
    p default message length = 26i = 16
    3else msg[i] + real_difference = 113
    q default message length = 26i = 17
    3else msg[i] + real_difference = 89 <== prob start
    Y default message length = 26i = 18
    3else msg[i] + real_difference = 90
    Z default message length = 26i = 19
    3else msg[i] + real_difference = 91
    [ default message length = 26i = 20
    3else msg[i] + real_difference = 92
    \ default message length = 26i = 21
    3else msg[i] + real_difference = 93
    ] default message length = 26i = 22
    3else msg[i] + real_difference = 94
    ^ default message length = 26i = 23
    3else msg[i] + real_difference = 95
    _ default message length = 26i = 24
    3else msg[i] + real_difference = 96
    ` default message length = 26i = 25
    3else msg[i] + real_difference = 97
    a default

    Press any key to continue . . .

    C:\Dev-Cpp\project333 misc>
    changed
    Code:
            while (i< msg.length()) //Converts each letter
                    {
    did not change anything in output.

    also tried if ( difference > 26 ) to if ( 26 > difference ) and
    26 < diff

    thank you for clearing the std
    confusion.
    Last edited by kryptkat; 12-08-2009 at 03:42 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Remember, you check for characters greater than 'z' because a character plus a positive offset can be greater than 'z'. But you also allow a negative offset. What is 'a' plus an offset of -9?

  8. #8
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i got it working. the way the original downloaded code snippet written it looks like it was never intended to be used to decrypt. the original questions is the same should code snippets work ? to learn from it is best to have a working example. opinion. it was the last part of the if line that was the problem and that it was not set up to undo.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Of course code snippets should work, but you shouldn't ever assume that they do, especially when you get them from some random place on the internet. Who knows who is putting these code snippets out there. Even pieces of code from professionals in programming books can have errors. They can also go out of date without being updated.

    The real point of a snippet of code is usually to show you an example. It is best if that example actually works, but you can still learn a lot from them if they don't.

    I don't know what "dreaming in c" is, so I can't comment on the particular place you found this code, but in general don't be surprised if you find things on the internet that aren't quite right.

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I agree; if you post code here (unless it is not working and you explain that in your submission) I think it had better work because to me giving a newbie code that doesn't work is far worse than giving them an answer that does. In the latter case the lazy ones will just take the code and run, not learning a thing whereas the curious ones will take the former and learn how to do it *wrong*....not what we are shooting for...

    Peace
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    kryptkat,

    A lot of authors write books for the money, and not because they care. There's a lot of absolutely crap C++ books out there, with absolutely terrible and failed examples of program source. That is clearly a case of a bad programmer writing what is probably also a bad book. It might also be a smart author designing snippets that require outside learning intentionally, to provoke you the learner to learn on your own as well; though any books I've seen like that the author explicitly states that is the intention with some examples.

  12. #12
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    In the latter case the lazy ones will just take the code and run
    guilty. prime example exactly what i did with abachlers prime gen code i entered it in his contest just to see what it would do. with the caesar i was using the decrypt to test the crypt to see if the encrypt worked correctly. it is getting harder to find any type of working encryption source code example or not.

    since

    i am trying to learn cpp better. there are a lot of little differences between c and cpp that can really confuse some one trying to learn. there are also a lot of good free pdf and zip ebooks like thinking in cpp and thinking in c and winforge tut and k and r . some good free compilers too. that come with some good working example code. code snippets on the other paw may be something closer to random if it works or not.

    That is clearly a case of a bad programmer writing what is probably also a bad book
    the code snippet that i downloaded i think was just to "fill" a start up web site. if they want more visits to their web site i think they should have working code examples for return visitors. opinion.
    bad coded examples in a book may not be a bad book entirely. kinda like void main() in books instead of int main().

    int meow().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-09-2007, 08:08 AM
  2. learning to work with SDKs & source code out there...
    By psasidisrcum in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2005, 09:26 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM