Thread: encripting a string

  1. #1
    j_spinto
    Guest

    encripting a string

    Code:
    int main() {
        char* string;
        FILE *fp;
        int i;
        int i2=0;
        printf("Insira o texto a codificar: ");
        scanf("%s",&string);
        while(string[i2]!='\n') {
                i2++;
        }
        string[i2]='\0';
        printf("\n");
        unsigned short int final[sizeof(string)];
        char key=(char)rand()%256;
        for(i=0; string[i]!='\0';i++) {
                final[i]=((int)string[i]*((int)key+1));
        }
        fp=fopen("keylog","a+");
        fputc(fp,(int)key);
        fclose(fp);
        printf("%hu\n",final);
        system("pause");
        return 0;
    }
    please say me whats wrong... i dont understand! :S

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What does it do or not do that you were or weren't expecting it to do?
    If you understand what you're doing, you're not learning anything.

  3. #3
    j_spinto
    Guest
    it gives me an error and asks me to send an error report to microsoft after entering the string lol

    the compiler warnings are those (theyre quite stupid, because the syntax is differente from the cppreference.com of the functions i use):
    in function `main'
    [warning] passing arg 1 of `fputc' makes integer from pointer without a cast
    [warning] passing arg 2 of `fputc' makes pointer from integer without a cast

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Your variable string is a pointer to char. It is not pointing anywhere (not even to NULL). You have not allocated any memory to it using malloc() or calloc(). This why, when you are trying to write into it. (or dereference it) using scanf(), its crashing the program after you enter the string. Your string pointer is trying to perform an illegal operation by trying to write into some one else's memory region. malloc() string first before using it in scanf().

    And yes, you dont need to use & operator when you are using string variable in scanf() since its a pointer.
    Last edited by nkhambal; 07-25-2005 at 04:43 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Try actually including the header files you're supposed to include.
    2) You have the arguments backwards. Read this.

    The warnings aren't "stupid", they're correct.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    j_spinto
    Guest
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main() {
        char string[BUFSIZ];
        FILE *fp;
        int i;
        int i2=0;
        printf("Insira o texto a codificar: ");
        scanf("%s",&string);
        while(string[i2]!='\n') {
                i2++;
        }
        string[i2]='\0';
        printf("\n");
        unsigned short int final[sizeof(string)];
        char key=(char)rand()%256;
        for(i=0; string[i]!='\0';i++) {
                final[i]=((int)string[i]*((int)key+1));
        }
        fp=fopen("keylog","a+");
        fputc(fp,(int)key);
        fclose(fp);
        printf("%hu\n",final);
        system("pause");
        return 0;
    }
    what's the matter now ? (i had already included the files but i didnt paste them)

  7. #7
    j_spinto
    Guest
    ups, i see.. i corrected the arguments but it ddoesnt work !

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    This will at least compile...
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main() {
        char string[BUFSIZ];
        FILE *fp;
        int i;
        int i2=0;
        printf("Insira o texto a codificar: ");
        scanf("%s",&string);
        while(string[i2]!='\n') {
                i2++;
        }
        string[i2]='\0';
        printf("\n");
        unsigned short int final[sizeof(string)];
        char key=(char)rand()%256;
        for(i=0; string[i]!='\0';i++) {
                final[i]=((int)string[i]*((int)key+1));
        }
        fp=fopen("keylog","a+");
        fputc((int)key, fp);
        fclose(fp);
        printf("%hu\n",final);
        system("pause");
        return 0;
    }
    Now see if it's doing what you want to do.

    EDIT:
    One more thing: you don't need the & operator when passing a pointer to an array of characters to scanf().
    Last edited by caduardo21; 07-26-2005 at 05:19 AM.

  9. #9
    j_spinto
    Guest
    still gives me an error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM