c - 如何检查密码直到正确?
问题描述
我正在编写一个程序来读取密码,直到密码正确为止。我对如何在此使用循环感到困惑。
#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.
}
解决方案
您的问题将使用无限循环来解决,该循环将一直运行,直到用户输入正确的密码。
因此,对于此任务,请附上您的 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");
}
}
}
推荐阅读
- java - 在没有 View Resolver 的情况下重定向到 spring-boot-starter-webflux 中的外部绝对 URL
- c# - 如何使用 Windows 服务调用浏览器选项卡
- android - 使用 Firebase 在我的 android 应用程序中验证两种类型的用户(学生和司机)的最佳方法是什么
- jquery - 我想从 gridview 中拖动一行并将其放在 treeview 上
- python-3.x - python3上numpy中float32数字的矩阵乘法不稳定性
- laravel-5.7 - Laravel 内部 API 调用控制器重定向到 localhost 后
- bpmn - 如何使用 BPMN 创建用户流程图?
- python - 无法在 sqlalchemy 中引用外键引用
- amazon-web-services - AWS 代码构建引发错误“CLIENT_ERROR:未找到主要源和源版本的参考增量”
- zend-framework3 - 如何在 ZF3 中初始化模块(用户)?