当前位置:   article > 正文

用C++简单实现密码设置与解锁_c++设置密码

c++设置密码

要求:

➢设置一个密码,允许数字和字母组合。
➢如果输入字符低于某个数值(比如6或8),提示:密码过短请重试。
➢连续输入3次错误密码,不再允许输入密码。
➢可以选择密码重置,将之前的密码覆盖。
 

实现:

1.设置密码

  1. void SetPassword(char * x) //此函数用于设置密码
  2. {
  3. cout << "Please set your password:" << endl;
  4. cin.getline(x, MAX); //从键盘输入不超过MAX位的字符作为密码
  5. if (strlen(x) < MIN) //判断输入密码长度,MIN为题目要求的密码最短位数
  6. {
  7. cout << "The password is too short.";
  8. SetPassword(x); //若密码输入过短,会再次要求输入
  9. }
  10. else
  11. cout << "The password is set successfully." << endl;
  12. cout << "------------------------------" << endl;
  13. }

2.输入密码

  1. void Decipher(char*y) //此函数用于解开密码
  2. {
  3. int i;
  4. char str[MAX];
  5. cout << "Please enter a password:" << endl;
  6. for (i = 0; i < 3; i++) {
  7. cin.getline(str, MAX); //从键盘输入一串字符
  8. if (strcmp(y, str) == 0) { //判断输入字符串和密码是否相同
  9. cout << "Deciphering successfully!" << endl;
  10. break;
  11. }
  12. else {
  13. cout << "Please enter your password again and you have "
  14. <<2-i <<" more chance." << endl;
  15. }
  16. }
  17. cout << "------------------------------" << endl;
  18. }

3.main函数部分

  1. int main()
  2. {
  3. int a = 0;
  4. char code[MAX];
  5. char* p1 = code;
  6. SetPassword( p1 );
  7. while (a != 3) {
  8. cout << "Please choose what you want to do next:" << endl
  9. << "1.Reset the password\t2.Unlock the password\t3.Come to an end" << endl;
  10. //1.重置密码 2.输入密码 3.结束程序
  11. cin >> a;
  12. getchar(); //把\n“吸”掉(可以去掉此行,运行程序,来进行对比)
  13. if (a == 1) {
  14. SetPassword(p1);
  15. }
  16. else if (a == 2) {
  17. Decipher( p1 );
  18. }
  19. }
  20. }

4.总程序

  1. #include <iostream>
  2. #include <string>
  3. #define MIN 6 //限定密码最小长度
  4. #define MAX 100
  5. using namespace std;
  6. void SetPassword(char * x)
  7. {
  8. cout << "Please set your password:" << endl;
  9. cin.getline(x, MAX);
  10. if (strlen(x) < MIN)
  11. {
  12. cout << "The password is too short.";
  13. SetPassword(x);
  14. }
  15. else
  16. cout << "The password is set successfully." << endl;
  17. cout << "------------------------------" << endl;
  18. }
  19. void Decipher(char*y)
  20. {
  21. int i;
  22. char str[MAX];
  23. cout << "Please enter a password:" << endl;
  24. for (i = 0; i < 3; i++) {
  25. cin.getline(str, MAX);
  26. if (strcmp(y, str) == 0) {
  27. cout << "Deciphering successfully!" << endl;
  28. break;
  29. }
  30. else {
  31. cout << "Please enter your password again and you have "
  32. <<2-i <<" more chance." << endl;
  33. }
  34. }
  35. cout << "------------------------------" << endl;
  36. }
  37. int main()
  38. {
  39. int a = 0;
  40. char code[MAX];
  41. char* p1 = code;
  42. SetPassword( p1 );
  43. while (a != 3) {
  44. cout << "Please choose what you want to do next:" << endl
  45. << "1.Reset the password\t2.Unlock the password\t3.Come to an end" << endl;
  46. cin >> a;
  47. getchar();
  48. if (a == 1) {
  49. SetPassword(p1);
  50. }
  51. else if (a == 2) {
  52. Decipher( p1 );
  53. }
  54. }
  55. }

运行结果: 

问题提出: 

 有大佬知道为什么这里会出现两条线吗?QAQ

这学期刚学C++ ,感觉头好痒,要长脑子了ᕦ༼༎ຶ_༎ຶ༽ᕗ

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/990881
推荐阅读
相关标签
  

闽ICP备14008679号