2016-05-07 14 views
-1

私は最初の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; 
        ^

..andこれは私のコードです:

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

答えて

1

あなたはあなたのコード内で多くのミスをしました。私はまずチュートリアルを作成することをお勧めします。あなたは何を書いているのか理解していなけれあなたのコードは試行錯誤のように見えます。誰かがあなたに実用的なコードを与えたら、それはあなたにとって有益だとは思わない。

+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関数を評価することはできません。私はそれを変更し、代わりにreturn文を入れました。この時

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

彼はする必要がありますCのチュートリアルに従ってください。 –

+0

多くの場合、プログラマの仕事はきれいなコンパイルを得ています。 –

+0

@ Programmer400 "それがうまくいくかどうか"はCの良いマントラではありません。 –

1

見て:あなたはcripta_password()

  • 方法cripta_password行う必要がありますので、cripta_passwordがメソッドである

    1. :ので

      int passwordfinale = cripta_password; 
      

      この文は、あまり意味があります返品無効...だからあなたはintにvoidを代入しないでください。コードを固定

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

    ようにすることができ、その後、その

    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]を
  • 1

    後に私が直接、この質問に答えるが、最初にあなたの過ちをクリーンアップしません:

    1 /インデント(ヘルプ用)

    2 /未使用コードを削除してください。

    3 /ポインタ機能未使用 - >削除。

    4 /期待どおりの戻り値の型intを追加し、returnステートメント。

    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); 
    } 
    
    関連する問題