rand()不起作用,给了我一个超出指定的最大值和最小值的数字
rand() not working, gives me a number outside of the specified max and minimum
所以我正在制作一个游戏,如果你是老板和雇佣员工,我以前使用过rand
,但由于某种原因,它现在不起作用:
生成随机数的代码:
class employee
{
public:
int getSkills(int askingPrice)
{
srand((unsigned)time(0));
switch(askingPrice)
{
case 1: //point of this is so if askingPrice is from 1 to 5 it will execute the same code
case 2:
case 3:
case 4:
case 5:
skills = rand() % 5 + 1;
}
return skills;
}
int returnSkills()
{
return skills;
}
int getPaid(int askingPrice)
{
srand((unsigned)time(0));
switch(askingPrice)
{
case 1:
case 2:
case 3:
case 4:
case 5:
paid = rand() % 5 + 1;
}
return paid;
}
int returnPaid()
{
return paid;
}
int getHappy(int askingPrice)
{
srand((unsigned)time(0));
switch(askingPrice)
{
case 1:
case 2:
case 3:
case 4:
case 5:
happy = rand() % 5 + 1;
}
return happy;
}
int returnHappy()
{
return happy;
}
private:
int skills, paid, happy;
};
显示随机数的代码:
std::cout <<"Your first employee is: " << one.returnPaid() << " salary, " << one.returnSkills() << " skilled @ his job, " << one.returnHappy() << " happy with his job";
出于某种原因,它显示为:
Your first employee is: -858993460 salary, 3 killed @ his job, 3 happy with his job
为什么我的工资是负数,而其他什么都没有?
我正在使用Visual Studio 2010
当您调用one.returnPaid()
时,one.paid
尚未初始化,因为one.getPaid()
(one.paid
将接收值的唯一函数)尚未被调用。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用递归的数组的最小值.这是怎么回事
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- txt 文件中浮点数的最大和最小值
- 余数运算符的等效操作,用于处理低于允许的最小值
- 需要使用模板查找数组的第二个最小和最小值
- 在没有比较运算符的情况下查找 2 个数字之间的最小值
- 如何打印用户在C++中输入的 10 个数字的最小值
- 从 3 个数字中选择最大值和最小值,C++
- 计算各种数字中的最小值倍数,并将其打印为prime
- rand()不起作用,给了我一个超出指定的最大值和最小值的数字
- 在C++中,我需要创建一个程序,该程序在输入某个数字时循环并停止,然后显示最大值和最小值
- 获取 2D 数组中数字的平均、最小值和最大值
- 使用 ifstream 读取列中随机生成的数字的文本文件,并从列表中查找最小值和最大值
- 为什么我的c++算法总是为一个数字的最小值和最大值返回相同的值