简体   繁体   English

为什么我的程序不起作用?

[英]Why my program doesn't work?

I'm creating my first C Program, this program must crypt your password. 我正在创建我的第一个C程序,该程序必须加密您的密码。 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). 1 /缩进(寻求帮助)。

2/ Remove unused code. 2 /删除未使用的代码。

3/ Pointer function unused -> remove. 3 /指针功能未使用->删除。

4/ Add return type int as expected, and return statement. 4 /按预期添加返回类型int,并返回语句。

5/ Correct type 5 /正确的类型

6/ Loop limit : here you loop through int (2^32), I can't correct as I don't know what you do. 6 /循环限制:在这里您循环遍历int(2 ^ 32),由于我不知道您的工作,我无法纠正。

7/ Add careful when add float and int... 7 /在添加float和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. 对于特殊情况(例如加密),有时可以将charint互换,但是必须使编译器满意。 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. 您编写了void ,因此无法评估void函数,因为void意味着您无法从该函数中获取值。 I changed it and put in a return statement instead. 我更改了它,然后放入return语句。

                          ^
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() cripta_password是一种方法,因此您应该做cripta_password()
  2. the method cripta_password return void... so you can not assign a void to an int. 方法cripta_password返回void ...,所以您不能将void分配给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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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