[英]Why my program doesn't work?
我正在創建我的第一個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);
}
好吧,我不直接回答這個問題,但請先清理您的錯誤:
1 /縮進(尋求幫助)。
2 /刪除未使用的代碼。
3 /指針功能未使用->刪除。
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);
}
您在代碼中犯了很多錯誤。 我建議先做一個交流教程。 您必須了解所寫內容。 您的代碼看起來更多是嘗試和錯誤。 如果有人給您工作代碼,我認為這對您沒有幫助。
我們收到警告:
$ 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);
=錯誤的類型。 只是要與類型保持一致。 對於特殊情況(例如加密),有時可以將char
和int
互換,但是必須使編譯器滿意。 下一個警告是相同的,類型不匹配:
^
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;
看這個:
int passwordfinale = cripta_password;
該語句沒有多大意義,因為:
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;
}
然后在那之后
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.