【问题标题】:How can I check the password until it is correct?如何检查密码直到正确?
【发布时间】:2021-04-28 15:34:55
【问题描述】:

我正在编写一个程序来读取密码,直到密码正确为止。我对如何在其中使用循环感到困惑。

#include <stdio.h>
#include <cs50.h> //cs library Harvard for getting input of string from the user
#include <string.h>

int main(void) 
{ 
    string first="hello"; 
    string check = get_string("Hello! \n, enter password ,"); //gets the string, input from the user.
    if (strcmp(first, check) == 0)
    { 
        printf("Welcome \n"); 
    } 
    else 
    {
        printf("\n wrong pwd, good bye \n");
    }//I want to put this part in loop until the correct pwd is entered.
} 

【问题讨论】:

    标签: c loops passwords cs50


    【解决方案1】:

    您的问题将使用无限循环解决,直到用户输入正确的密码。
    因此,对于这个任务,请附上您的 if-else 语句并在无限的 while 循环中打印,一旦用户获得正确的密码,该循环就会中断。

    #include <stdio.h>
    #include <cs50.h> 
    #include <string.h>
    
    int main(void) 
    { 
      string first="hello"; 
      while(1)
      {
        string check = get_string("Hello! \n, enter password ,");
        if (strcmp(first, check) == 0)
        { 
          printf("Welcome \n"); 
          break;
        }
        else 
        {
          printf("\n wrong pwd, good bye \n");
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 2015-12-28
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      • 2016-09-19
      相关资源
      最近更新 更多