所以我正在嘗試構建一個 ATM 程式,但我真的不明白為什么我的代碼不起作用。
代碼:
#include <stdio.h>
#include <stdbool.h>
bool accessCheck(int);
const int pin =1234;
int main(){
int code, access;
printf("Hi, please enter your password:");
scanf("%d",&code);
accessCheck(code);
if(accessCheck(code)==1){
printf("Password recognized.");
}
else {
printf("Password unrecognized.");
}
return 0;
}
bool accessCheck(int x){
bool access=0;
if(x == pin){
bool access = 1;
}
return access;
}
uj5u.com熱心網友回復:
您正在塊access內宣告新變數if。這會在accessCheck.
您應該只分配該變數而不是宣告另一個變數。
#include <stdio.h>
#include <stdbool.h>
bool accessCheck(int);
const int pin =1234;
int main(){
int code;
printf("Hi, please enter your password:");
scanf("%d",&code);
if(accessCheck(code)){
printf("Password recognized.");
}
else {
printf("Password unrecognized.");
}
return 0;
}
bool accessCheck(int x){
bool access=false;
if(x == pin){
access = true;
}
return access;
}
由于您正在使用stdbool,因此可以使用trueandfalse代替1and 0。
沒有必要accessCheck()在if宣告之前呼叫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/336474.html
標籤:C
下一篇:我如何使用指標作為引數
