numeric_limits问题
#include<limits>#include<iostream>usingstd::cout;usingstd::endl;usingstd::numeric_lim...
#include <limits>
#include <iostream>
using std::cout;
using std::endl;
using std::numeric_limits;
int main() {
cout << endl
<< "The range for type short is from "
<< numeric_limits<short>::min()
<< " to "
<< numeric_limits<short>::max();
return 0;
}
提示:error C2653: 'numeric_limits<short>' : is not a class or namespace name
但当我用using namespace std;代替“using std::cout;
using std::endl;
using std::numeric_limits;
”时就能运行正确了,为什么?
我在做一个题目:创建一个程序,输出由8个随机大小的字母或数字组成的密码。允许输入重复的字符。代码如下:
#include <iostream>
#include <cctype>
#include <limits>
#include <ctime>
#include<cstdlib>
using namespace std;
int main() {
const int chars_in_password= 8;
char ch = 0;
srand(static_cast<unsigned int>(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())*rand())/RAND_MAX;
if(!isalnum(ch))
continue;
cout << ch;
break;
}
cout << endl;
return 0;
}
目前问题是:能正常运行,但最后输入的不是字母,而是乱码,何解? 展开
#include <iostream>
using std::cout;
using std::endl;
using std::numeric_limits;
int main() {
cout << endl
<< "The range for type short is from "
<< numeric_limits<short>::min()
<< " to "
<< numeric_limits<short>::max();
return 0;
}
提示:error C2653: 'numeric_limits<short>' : is not a class or namespace name
但当我用using namespace std;代替“using std::cout;
using std::endl;
using std::numeric_limits;
”时就能运行正确了,为什么?
我在做一个题目:创建一个程序,输出由8个随机大小的字母或数字组成的密码。允许输入重复的字符。代码如下:
#include <iostream>
#include <cctype>
#include <limits>
#include <ctime>
#include<cstdlib>
using namespace std;
int main() {
const int chars_in_password= 8;
char ch = 0;
srand(static_cast<unsigned int>(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())*rand())/RAND_MAX;
if(!isalnum(ch))
continue;
cout << ch;
break;
}
cout << endl;
return 0;
}
目前问题是:能正常运行,但最后输入的不是字母,而是乱码,何解? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询