c - 修改密码C

标签 c passwords

我是编程新手,我正在尝试找出为什么我的密码没有更改。我尝试移动 pin=1234;在 main(void) 中,但这没有任何作用。我有什么想法可以做到这一点吗?

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

我猜测 pin 更改后,它会再次变为 pin=1234,因为这是他的初始值。但我怎样才能改变它,不让我的初始密码为 1234 呢?

最佳答案

无法保存新 pin 的原因是每次进入 getPin() 函数时都将 1234 分配给“pin”变量。在changePin()中更改密码后,您正在调用getPin()。

将“pin”设置为全局变量,然后就可以开始了。

您的代码在 while 语句中存在另一个问题。 = 是赋值运算符。你想要做的是while(1)

这是代码:

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

这是一个示例输出:

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

希望这有帮助。

巴里斯

关于c - 修改密码C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369634/

相关文章:

c - C中的 float 与小数

c - 如何通过更改符号以保留方式将数组复制到另一个数组

c - 这句话的含义是什么 t=(input[i]= ='t' || input[i]= ='T' );

python - 在数据库中存储用户和密码

用于密码管理的 Java 框架

php - 不使用 SSL 对密码进行哈希处理的最佳实践

c - 打印整数和 float 的运算结果

c - 将指向内存的指针初始化为 "handles"

ruby-on-rails - Ruby on Rails - 使用 md5 的 Postgresql - Rake 中止! fe_sendauth : no password supplied

android - LDAP 更改 Active Directory 上的用户密码