fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. string password_var;
  9. bool validPass;
  10. int Length_Of;
  11.  
  12.  
  13. while (1)
  14. {
  15. cout << "Please enter a password.\n";
  16. cin >> password_var;
  17. validPass = false;
  18. Length_Of = password_var.size();
  19. if (Length_Of < 7)
  20. {
  21. cout << "Passwords must be at least 7 characters long.\n";
  22. continue; // you don't need to go further in the loop.
  23. }
  24. int i = 0;
  25. while (1)
  26. {
  27. cout << password_var[i];
  28. if ( (password_var[i] >= '0'&&password_var[i] <= '9')
  29. || (password_var[i] == '$')
  30. )
  31. {
  32. validPass = true;
  33. }
  34. else
  35. {
  36. i = i + 1;
  37. }
  38. // beware of assignment '=' and comparison '=='
  39. if (validPass == true || i >= Length_Of)
  40. break;
  41. }
  42. if (validPass == true)
  43. {
  44. cout << "Thank you, that is a valid password.\n";
  45. } // add the closing brace.
  46. else
  47. {
  48. cout << "Passwords must include a digit or a dollar sign(0-9,$).\n";
  49. }
  50. if (validPass == true)
  51. break;
  52. }
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 3432KB
stdin
1213c
asd$jk
abhishek
asbhi$sdasd
stdout
Please enter a password.
Passwords must be at least 7 characters long.
Please enter a password.
Passwords must be at least 7 characters long.
Please enter a password.
abhishekPasswords must include a digit or a dollar sign(0-9,$).
Please enter a password.
asbhi$Thank you, that is a valid password.