📄 ex5_04.cpp
字号:
// Exercise 5.4 Generating a random password
#include <iostream>
#include <cctype>
#include <limits>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
using std::numeric_limits;
int main() {
const int chars_in_password= 8;
char ch = 0;
std::srand(static_cast<unsigned int>(std::time(NULL)));
cout << "Your password is: ";
// We have to read at least one character - even if it's '#' - so do-while is best
for (int i = 0 ; i< chars_in_password ; i++)
while(true) {
// Character code is a random value between the minimum and maximum for type char
ch = numeric_limits<char>::min()+((numeric_limits<char>::max()-numeric_limits<char>::min())*std::rand())/RAND_MAX;
if(!std::isalnum(ch))
continue;
cout << ch;
break;
}
cout << endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -