简体   繁体   中英

Why my program doesn't work?

I'm creating my first C Program, this program must crypt your password. But there's a problem: give me many error, But I don't know how to fix it. Anyone could fix it and say me why my program doesn't run? These are the errors:

psw-crypter.C: In function ‘void cripta_password()’:
psw-crypter.C:6:41: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘char*’ [-Wformat=]
                 int psw = scanf("%d", &i);
                                         ^
psw-crypter.C: In function ‘int main()’:
psw-crypter.C:18:26: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘char*’ [-Wformat=]
  int psw = scanf("%d", &i);
                          ^
psw-crypter.C:19:23: error: invalid conversion from ‘void (*)()’ to ‘int’ [-fpermissive]
  int passwordfinale = cripta_password;
                       ^

..and this is my code:

#include <stdio.h>


void cripta_password() {
        char i;
        int psw = scanf("%d", &i);
        int psw1 = psw %  10;
        for(int number = psw1; number < psw1;psw1++) {
        int psw2 = psw1 * psw1;
        int psw3 = psw2 % psw1;
        int pswcript = psw3 + 3.14;
}
}

int main() {
printf("Cripta la tua password!");
char i;
int psw = scanf("%d", &i);
int passwordfinale = cripta_password;
printf("La tua password completa è:");
printf("%d", passwordfinale);
}

Ok I don't answer directly this question but first clean up your mistakes :

1/ Indent (it's for help).

2/ Remove unused code.

3/ Pointer function unused -> remove.

4/ Add return type int as expected, and return statement.

5/ Correct type

6/ Loop limit : here you loop through int (2^32), I can't correct as I don't know what you do.

7/ Add careful when add float and int...

#include <stdio.h>

// #4 void cripta_password() {
int cripta_password() {
    // #5 char i;
    int i;
    int psw = scanf("%d", &i);
    int psw1 = psw %  10;
    // #6 you loop throught int (2^32)
    for(int number = psw1; number < psw1;psw1++) {
        int psw2 = psw1 * psw1;
        int psw3 = psw2 % psw1;
        // #7 cast addition int + float...
        int pswcript = psw3 + 3.14;
    }
    return 1; // #6 return your int var
}

int main() {
    printf("Cripta la tua password!");
    // #2 char i;
    // #2 int psw = scanf("%d", &i);
    // #3 int passwordfinale = cripta_password;
    printf("La tua password completa è:");
    printf("%d", cripta_password()); // #3 passwordfinale);
}

You made a lot of mistakes in your code. I suggest to make ac tutorial first. You must understand what you write. Your code looks more as try and error. I don't think it's helpfull for you if someone gives you a working code.

We got warnings:

$ gcc crypt.c 
crypt.c: In function ‘cripta_password’:
crypt.c:6:25: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
         int psw = scanf("%d", &i);
                         ^
crypt.c: In function ‘main’:
crypt.c:18:17: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
 int psw = scanf("%d", &i);
                 ^
crypt.c:19:22: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
 int passwordfinale = cripta_password;

Just fix the warnings and call your function properly and use correct types for everything:

#include <stdio.h>

int cripta_password() {
    char i;
    int pswcript;
    int psw = scanf("%c", &i);
    int psw1 = psw % 10;
    for (int number = psw1; number < psw1; psw1++) {
        int psw2 = psw1 * psw1;
        int psw3 = psw2 % psw1;
        pswcript = psw3 + 3.14;
    }
    return pswcript;
}

int main() {
    printf("Cripta la tua password!");
    char i;
    int psw = scanf("%c", &i);
    int passwordfinale = cripta_password();
    printf("La tua password completa è:");
    printf("%d", passwordfinale);
}

Now test it:

crypta
Cripta la tua password!niklas
La tua password completa è:23
Process finished with exit code 0

The errors you had mean:

psw-crypter.C: In function ‘void cripta_password()’:
psw-crypter.C:6:41: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘char*’ [-Wformat=]
                 int psw = scanf("%d", &i);

= wrong type. Just be consistent with types. You can sometimes interchange char and int for special cases (like encryption) but you must please the compiler. The next warning is the same, the types don't match:

                                         ^
psw-crypter.C: In function ‘int main()’:
psw-crypter.C:18:26: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘char*’ [-Wformat=]
  int psw = scanf("%d", &i);

Then the following error is important. Your function has to return a value if you evaulate the function as a value. You wrote void , you can't evaluate a void function because void means that you don't get a value from the function. I changed it and put in a return statement instead.

                          ^
psw-crypter.C:19:23: error: invalid conversion from ‘void (*)()’ to ‘int’ [-fpermissive]
  int passwordfinale = cripta_password;

look at this:

int passwordfinale = cripta_password;

this statement makes not much sense because:

  1. cripta_password is a method so you should do cripta_password()
  2. the method cripta_password return void... so you can not assign a void to an int.

fixing the code can be like

int cripta_password() {
        char i;
        int psw = scanf("%d", &i);
        int psw1 = psw %  10;
        for(int number = psw1; number < psw1;psw1++) {
        int psw2 = psw1 * psw1;
        int psw3 = psw2 % psw1;
        int pswcript = psw3 + 3.14;
        return pswcript;
}

and then after that

int main() {
    printf("Cripta la tua password!");
    char i;
    int psw = scanf("%d", &i);
    int passwordfinale = cripta_password();
    printf("La tua password completa è:");
    printf("%d", passwordfinale);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM