2017-11-18 5 views
0

프로그래밍에 익숙하지 않아서 비밀번호가 변경되지 않는 이유를 파악하려고합니다. 나는 움직이는 핀을 시도했다 = 1234; 메인 (무효)에 있지만 아무것도하지 않습니다. 어떤 아이디어라도 어떻게 할 수 있습니까?비밀번호 변경 C

#include <stdio.h> 
#include <string.h> 
void getPin(); 
void changePin(); 

int main(void) 
{ 
    getPin(); 
} 

void getPin() 
{ 
    int pin; 
    pin=1234; 
    int findPin; 
    printf("What is the pin?: "); 
    scanf("%d", &findPin); 
    int x=1; 
    while(x=1) 
    { 
    if (findPin == pin) 
     { 
     printf("\nAcces granted\n"); 
     changePin(); 
     break; 
     } 
    else 
     { 
     printf("\nAcces denied\n"); 
     printf("What is the pin?: "); 
     scanf("%d", &findPin); 
     x=1; 
     } 
    } 
} 

void changePin() 
{ 
    int pin; 
    int newPin; 
    printf("\nEnter new pin: "); 
    scanf("%d", &newPin); 
    pin = newPin; 
    printf("\nThe new pin is: %d\n", pin); 

    getPin(); 
} 

나는 핀이 변경된 후에는 그 자신의 초기 값이기 때문에 = 1234 핀 다시 간다 같은데요. 하지만 1234에 처음 핀을 갖지 않기 위해 어떻게 바꿀 수 있습니까?

+0

'='할당과 '=='과 같은지 비교하는 차이점을 알고 있습니까? 나는 당신의 루프'while (x = 1) 때문에 궁금하다. –

+0

또한 서로 다른 스코프 (다른 함수와 같은)의 다른 변수는 이름이 같더라도 서로 관련이 없다는 것을 알고 있습니까? –

+0

예, 비밀번호를 변경 한 후에 getPin()에서 pin = 1234를 선언하면 다시 값 1234가 적용됩니다. 1234 값을 변경하지 못하게하려면 어떻게해야합니까? –

답변

0

새 핀을 저장할 수없는 이유는 getPin() 함수를 입력 할 때마다 1234를 "pin"변수에 할당하기 때문입니다. changePin()에서 암호를 변경 한 후에 getPin()을 호출합니다.

"핀"을 전역 변수로 만드십시오.

코드에 while 문과 관련된 다른 문제가 있습니다. =은 대입 연산자입니다. 원하는 것은 while(1)입니다. 이 도움이

What is the pin?: 1234 

Acces granted 

Enter new pin: 5555 

The new pin is: 5555 
What is the pin?: 1235 

Acces denied 
What is the pin?: 1234 

Acces denied 
What is the pin?: 55555 

Acces denied 
What is the pin?: 5555 

Acces granted 

Enter new pin: 3333 

The new pin is: 3333 
What is the pin?: 5555 

Acces denied 
What is the pin?: 1234 

Acces denied 
What is the pin?: 3333 

Acces granted 

희망 :

여기
#include <stdio.h> 
#include <string.h> 
void getPin(); 
void changePin(); 

int pin = 1234; // Default pin 

int main(void) 
{ 
    getPin(); 
} 

void getPin() 
{ 
    int findPin; 
    printf("What is the pin?: "); 
    scanf("%d", &findPin); 
    while(1) 
    { 
    if (findPin == pin) 
     { 
     printf("\nAcces granted\n"); 
     changePin(); 
     break; 
     } 
    else 
     { 
     printf("\nAcces denied\n"); 
     printf("What is the pin?: "); 
     scanf("%d", &findPin); 
     } 
    } 
} 

void changePin() 
{ 
    int newPin; 
    printf("\nEnter new pin: "); 
    scanf("%d", &newPin); 
    pin = newPin; 
    printf("\nThe new pin is: %d\n", pin); 

    getPin(); 
} 

은 샘플 출력입니다 : 여기

는 코드입니다.

바리스