里面的函数问题

sycocacola 2013-10-11 10:15:11
题目:创建一个程序,输出由8个随机大小写字母或数字组成的密码。允许输入重复的字符。必须用循环来做。

#include <iostream>
#include <cstdlib>
#include <cctype>
#include <limits>
#include <ctime>
using namespace std;
int main() {
const int chars_in_password= 8;
char ch = 0;
srand(static_cast<unsigned int>(time(0)));

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())*rand())/RAND_MAX);
if(!isalnum(ch))
continue;
else{
cout<<ch;
break;
}

}
cout << endl;
return 0;
}

关键问题是,标记红色的那一块,总是输出奇奇怪怪的字符,我试了一下即便把ch强制转换成char或者int型,让其只输出字母,或者只输出数字,也还是会有那些奇怪的字符,总之,只要用了cctype里的isdigit()或者isalpha()就出问题,输出很奇怪,最后无奈改成:

#include <iostream>
#include <cstdlib>
#include <cctype>
#include <limits>
#include <ctime>
using namespace std;
int main() {
const int chars_in_password= 8;
char ch = 0;
srand(static_cast<unsigned int>(time(0)));

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())*rand())/RAND_MAX);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
cout<< ch;
break;
}
else if(ch>=0&&ch<=9){
cout<<static_cast<int>(ch);
break;
}
else
continue;


}

cout << endl;
return 0;
}

红色标记的是改了之后的,就可以出现想要的结果了,我不明白,为什么调用cctype里的函数会出现这种问题呢?即便强制转换也不行!
...全文
135 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Falleyes 2013-10-17
  • 打赏
  • 举报
回复
这个可以直接char ch之后和ASC码的对应数字比较,做减法来输出字符。
赵4老师 2013-10-11
  • 打赏
  • 举报
回复
Locale Use the setlocale function to change or query some or all of the current program locale information. “Locale” refers to the locality (the country and language) for which you can customize certain aspects of your program. Some locale-dependent categories include the formatting of dates and the display format for monetary values. For more information, see Locale Categories. Locale-Dependent Routines Routine Use setlocale Category Setting Dependence atof, atoi, atol Convert character to floating-point, integer, or long integer value, respectively LC_NUMERIC is Routines Test given integer for particular condition. LC_CTYPE isleadbyte Test for lead byte () LC_CTYPE localeconv Read appropriate values for formatting numeric quantities LC_MONETARY, LC_NUMERIC MB_CUR_MAX Maximum length in bytes of any multibyte character in current locale (macro defined in STDLIB.H) LC_CTYPE _mbccpy Copy one multibyte character LC_CTYPE _mbclen Return length, in bytes, of given multibyte character LC_CTYPE mblen Validate and return number of bytes in multibyte character LC_CTYPE _mbstrlen For multibyte-character strings: validate each character in string; return string length LC_CTYPE mbstowcs Convert sequence of multibyte characters to corresponding sequence of wide characters LC_CTYPE mbtowc Convert multibyte character to corresponding wide character LC_CTYPE printf functions Write formatted output LC_NUMERIC (determines radix character output) scanf functions Read formatted input LC_NUMERIC (determines radix character recognition) setlocale, _wsetlocale Select locale for program Not applicable strcoll, wcscoll Compare characters of two strings LC_COLLATE _stricoll, _wcsicoll Compare characters of two strings (case insensitive) LC_COLLATE _strncoll, _wcsncoll Compare first n characters of two strings LC_COLLATE _strnicoll, _wcsnicoll Compare first n characters of two strings (case insensitive) LC_COLLATE strftime, wcsftime Format date and time value according to supplied format argument LC_TIME _strlwr Convert, in place, each uppercase letter in given string to lowercase LC_CTYPE strtod, wcstod, strtol, wcstol, strtoul, wcstoul Convert character string to double, long, or unsigned long value LC_NUMERIC (determines radix character recognition) _strupr Convert, in place, each lowercase letter in string to uppercase LC_CTYPE strxfrm, wcsxfrm Transform string into collated form according to locale LC_COLLATE tolower, towlower Convert given character to corresponding lowercase character LC_CTYPE toupper, towupper Convert given character to corresponding uppercase letter LC_CTYPE wcstombs Convert sequence of wide characters to corresponding sequence of multibyte characters LC_CTYPE wctomb Convert wide character to corresponding multibyte character LC_CTYPE _wtoi, _wtol Convert wide-character string to int or long LC_NUMERIC

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧