Thread: Encryption program inside...

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Arrow Encryption program inside...

    Hello,

    I know this is a bad program and I have thrown in many bugs related to type conversion. I am getting a warning also. Can you help me correct it ?

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        char ch[256];
        int cha[256];
        int count;
        printf("Enter a sentence:");
        fgets(ch,256,stdin);
        printf("\n%s",ch);
        for(count=0;ch[count]!='\0';count++)
        {
                                            cha[count]=((int)ch[count]+2);      
        }
        cha[count]=0;
        printf("\n After Encryption:  ");
            /*     for(count=0;cha[count]!=0;count++)
        {
                                          printf("%c",cha[count]);
        }*/
        
        puts((char)cha); /* [Warning] cast from pointer to integer of different size */
        getch();
        return 0;
    }
    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is this program supposed to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Print it the same way that you did the original.
    It seems to be a string ending in a nul.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by laserlight View Post
    What is this program supposed to do?
    Sorry.

    It is supposed to take input string and then make it a jumbled secret code. It should add 2 to the ASCII value of each character.

    Like A(=97) becomes B (=98).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, so I'm guessing that the reason that you dont want to just do a printf() like earlier in the code is that you want to display the ASCII values of the "encrypted" characters?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by Salem View Post
    Print it the same way that you did the original.
    It seems to be a string ending in a nul.
    Yes, it is printing a special character (A circle with an arrow pointing downwards) after the encrypted string. But it was doing the same with printf so i commented it out and used puts, thought it would be better to use puts.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    you want to display the ASCII values of the "encrypted" characters?
    No I want to display the characters, not their ASCII value.

  8. #8
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    If I input ABC it should give CDE.

    But it is giving a special character (A circle with an arrow pointing downwards) after the E.
    Last edited by abh!shek; 02-02-2008 at 01:36 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, that is much clearer now.

    Basically, you only need one string. You do not need an array of ints. Then you can just write:
    Code:
    for (count = 0; cha[count] != '\0' && cha[count] != '\n'; count++)
    {
        cha[count] += 2;
    }
    cha[count] = '\0';
    printf("\nAfter Encryption: &#37;s", cha);
    The catch is that if your string contains the characters '}' or '~', you will end up with unprintable characters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But it is giving a special character (A circle with an arrow pointing downwards) after the D.
    That would be your encrypted version of the \n at the end of the line (as returned by fgets)

    Edit:
    Beaten.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Yes its working now. Thanks!
    Last edited by abh!shek; 02-02-2008 at 01:16 AM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Quote Originally Posted by laserlight View Post
    Ah, that is much clearer now.

    Basically, you only need one string. You do not need an array of ints. Then you can just write:
    Code:
    for (count = 0; cha[count] != '\0' && cha[count] != '\n'; count++)
    {
        cha[count] += 2;
    }
    cha[count] = '\0';
    printf("\nAfter Encryption: &#37;s", cha);
    The catch is that if your string contains the characters '}' or '~', you will end up with unprintable characters.
    I think you were supposed to use ch[] not cha[]. And you can fix the problem of non-printable characters by putting an if statement:
    Code:
    for (count =0; ch[count] != '\0' && ch[count] != '\n'; count++)
    {
        if (ch[count] >= sizeof(char) - 2)
            ch[count] -= sizeof(char)-2;
        else
            ch[count] += 2;
    }
    Last edited by Abda92; 02-02-2008 at 05:21 AM.

  13. #13
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by laserlight View Post

    The catch is that if your string contains the characters '}' or '~', you will end up with unprintable characters.

    Why are } and ~ unprintable ?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why are } and ~ unprintable ?
    They are printable, but when you add 2 to them, the result is not printable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by laserlight View Post
    They are printable, but when you add 2 to them, the result is not printable.
    I still don't get it.
    '}' has ASCII value 125.
    And 127 is a triangle. So it should print a triangle..

    ~ has value 126
    and 128 is C (not the regular C, it has slightly different shape), so it should print that C....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 08:56 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  3. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  4. program design question
    By theroguechemist in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2004, 08:45 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM