c程序允许用户更改已经设置的4位引脚


  1. 输入PIN并验证是否正确
  2. 更改PIN
  3. 显示输入PIN的次数(i)成功(ii)不正确地
  4. 退出程序

我有问题,在上面的问题中,选项2应该做以下事情:如果他们选择选项2,则应允许他们更改PIN。程序应首先验证原始引脚。当他们输入新的PIN时,您的程序必须通过询问客户重新输入此新PIN。这将验证输入的新PIN是正确的,没有出现任何错误。如果有任何差异验证失败,程序必须显示相应的错误消息并且原始PIN应该保持不变。

当用户输入他们的新密码1时,我的问题就出现了。引脚长度必须为4位数字2。如果用户在输入新接点时输入了错误的接点,则原始接点必须保持不变。如果他们正确输入他们的新引脚,那么他们的新针脚必须更改为该针脚。到目前为止,我已经发布了选项2的代码。

如果我的问题不清楚,请问我一些问题。

case 2:
      {
           //ask user to enter their current pin
           printf("Please enter your current pin n");
           scanf("%d",&current_pin);

           if(current_pin != 1234)
           {
                //if pin entered is not the same as 1234-print error
                printf("Incorrect pin n");
                unsuccessful ++;
                break;
           }//end if    
           else
            {
                successful++; 
                //ask user to enter new pin
                printf("Please enter your new pin: n");
                scanf("%d",&new_pin);           
            } //end else
            //set new pin as the current pin
            current_pin = new_pin:      
            //check if pin is 4 digits long
            if(current_pin>999 && current_pin<10000)
            {
                //ask user to re enter their new pin
                printf("Please re-enter your new pin: n");
                scanf("%d",&new_pin);
                printf("Your new pin is %d", new_pin);
                //set new pin as the current pin
                current_pin = new_pin:  
            }   
           else
            {
                printf("Incorrect entry- pin must be 4 digits and cannot start with a 0");                          
            }
                   new_pin=current_pin;
              break;
            }

我知道这个代码很难理解,我希望你能理解它。我知道我想做什么,但我做不到。

****编辑****好吧,我现在明白了,我让你们中的一些人感到困惑。也许我应该改写我的问题。并将其分解为多个部分。如果比较容易的话,我可以发布程序的完整代码。

  1. 我希望用户输入他们的当前引脚,如果程序是第一次运行的,则该引脚已被分配值1234。如果用户已经完成了该过程,则当前引脚将是他们将其更改为的引脚。

  2. 引脚新引脚必须为4位

  3. 这可能会推动它,但如果用户在我的程序中输入一个字母,它将进入一个无限循环,有简单的方法吗。

这有点奇怪:

   printf("Please enter your current pin n");
   scanf("%d",&current_pin);
   if(current_pin != 1234)
   {

current_pin是存储用户pin的值吗?如果是这样,您将在扫描时更改它。创建一个新的变量来存储结果。类似这样的东西:

   int value;
   scanf("%d",&value);
   if(current_pin != value)
   {

你似乎经常这样做:

current_pin = new_pin:

我承认,我有点困惑,结肠是打字错误还是做了一些我不明白的事情。

然而,在检查新引脚的值之前,您不应该执行此任务

//check if pin is 4 digits long
if(new_pin>999 && new_pin<10000)
{
    int verified;
    //ask user to re enter their new pin
    printf("Please re-enter your new pin: n");
    scanf("%d",&verified);
    if(verified == new_pin))
    {
        printf("Your new pin is %d", new_pin);
        //set new pin as the current pin
        current_pin = new_pin; // <--- This is the only place you assign to current_pin 
    }
    else
    {
        //tell the user there was an error
    }
}

在您的第一个if语句中

if (current_pin != 1234)

您正在将current_pin与常数值1234进行比较尝试使用变量而不是1234常量。

您从未检查过重新输入的newpin是否与newpin相同。

最新更新