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

时间:2016-05-07 20:07:46

标签: c debugging cryptography

我正在创建我的第一个C程序,该程序必须加密您的密码。但是有一个问题:给我许多错误,但我不知道如何解决它。 任何人都可以解决它,并告诉我为什么我的程序不运行? 这些是错误:

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;
                       ^

..这是我的代码:

#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);
}

4 个答案:

答案 0 :(得分:1)

你的代码犯了很多错误。我建议先做一个c教程。你必须明白你写的东西。您的代码看起来更像是尝试和错误。如果有人为您提供有效的代码,我认为这对您没有帮助。

答案 1 :(得分:1)

我们收到了警告:

$ 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;

只需修正警告并正确调用您的功能,并为所有内容使用正确的类型:

#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);
}

现在测试一下:

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

你所犯的错误意味着:

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);

=错误的类型。只是与类型一致。对于特殊情况(如加密),您有时可以互换charint,但您必须取悦编译器。下一个警告是相同的,类型不匹配:

                                         ^
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);

然后以下错误很重要。如果将函数作为值进行评估,则函数必须返回一个值。您撰写了void,无法评估void函数,因为void表示您没有从函数中获取值。我更改了它并改为返回声明。

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

答案 2 :(得分:1)

看看这个:

class Test {

    static class Bar {
        private List<Integer> integers;
        private List<String> strings;
    }

    public static void main(String[] argv) {
        Type baseType = new TypeToken<List<Bar>>() {}.getType();
        List<Bar> foos = new ArrayList<>();

        Bar bar;

        bar = new Bar();
        bar.integers = Arrays.asList(1, 2, 3, 4);
        bar.strings = Arrays.asList("a", "b", "c", "d");
        foos.add(bar);

        bar = new Bar();
        bar.integers = Arrays.asList(5, 6, 7, 2131558489);
        bar.strings = Arrays.asList("e", "f", "g", "h");
        foos.add(bar);

        Gson gson = new Gson();
        String tmp = gson.toJson(foos, baseType);
        System.out.println(tmp);
        foos = gson.fromJson(tmp, baseType);
        System.out.print(foos.get(1).integers.get(3));
    }
}

这句话没有多大意义,因为:

  1. cripta_password 是一种方法,因此您应该int passwordfinale = cripta_password;
  2. 方法 cripta_password 返回void ...因此您无法为int分配void。
  3. 修复代码可以像

    cripta_password()

    然后在那之后

    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;
    }
    

答案 3 :(得分:1)

好的,我不是直接回答这个问题,而是先清除你的错误:

1 /缩进(它的帮助)。

2 /删除未使用的代码。

3 /指针功能未使用 - &gt;除去。

4 /按预期添加返回类型int,并返回语句。

5 /正确类型

6 /循环限制:在这里你循环通过int(2 ^ 32),我无法纠正,因为我不知道你做了什么。

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);
}