【发布时间】:2015-02-23 09:43:52
【问题描述】:
我正在编写一个带有返回随机数的函数的代码。随机数是从 0 到我输入的最大数生成的。除此之外,我必须生成 3 个随机数,然后随机数将返回 char '-' '+' '*' 。代码不能正常工作。每当我输入 100 时,生成的随机数始终为 41,而生成 char 的另一个随机数始终生成 - 带有笑脸的符号(不是开玩笑,char 符号旁边会出现一个笑脸)。谁能告诉我我的代码有什么问题?非常感谢您帮助我。
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int getNumber(int);
char getOperator(int);
int max = 0;
int randOperator = 0;
cout << "Enter a value(1- 101)" << endl;
cin >> max;
while((max < 0) || (max > 101))
{
cout << "invalid value. Please enter a value (1 - 101)" << endl;
cin >> max;
}
cout << endl;
cout << getNumber(max) << endl;
cout << getOperator(randOperator);
}
int getNumber(int max)
{
int hehe;
hehe = rand()% max;
return hehe;
}
char getOperator(int randOperator)
{
for(int x = 0; x < 5; x++)
{
randOperator = rand() % 3;
if(randOperator = 0)
{
cout << '+';
}else if(randOperator = 1)
{
cout << '-';
}else
{
cout << '*';
}
return randOperator;
}
}
【问题讨论】:
-
用
srand()播种你的rand。您可以在谷歌中搜索它。你会得到答案的。
标签: c++