c++ - 我正在尝试创建一个程序,该程序根据用户输入的最大值来乘以随机数

标签 c++ random

我还不太擅长这个,我正在尝试学习如何让用户声明的变量在我的方程式中起作用。 现在,我只希望计算机根据用户指定的最大数字进行随机乘法运算。

当我尝试运行它时,机器会吐出这些错误:

12:16: error: ambiguous overload for 'operator>>' in 'std::cin >> 32767'

14:61: error: 'x' was not declared in this scope

14:64: error: 'y' was not declared in this scope

15:16: error: statement cannot resolve address of overloaded function

20:9: error: declaration of 'int x' shadows a parameter

21:5: error: expected ',' or ';' before 'int

最终目标是计算机将在难度参数内生成问题,然后删除方程式中的一个变量来测验用户。

#include <cstdlib>
#include <iostream>

using namespace std;

int mult( int x, int y );

int main()
{
    cout <<"Please enter a number between 2 and 21, this will determine how difficult your problems are.";
    cin >> RAND_MAX;
    cin.ignore();
    cout << "The product of your numbers is:" << mult ( x, y ) <<"\n";
    cin.get;
}

int mult (int x, int y)
{
    int x = rand()
    int y = rand()
    return  x * y;
}

最佳答案

这里有不少错误。我会尽量友善。

  1. 您的两个 rand() 调用后都需要分号。
  2. xy 没有在 main() 的任何地方声明。我不知道您为什么要将它们作为参数传递给 mult(),但我认为 future 会有一些相关的功能。
  3. RAND_MAX 是一个常量,所以 cin >> RAND_MAX 没有意义。相反,请参阅@Bill 的回答。
  4. cin.get 后需要括号。

这是一个工作示例,希望这是您希望它执行的操作:

#include <cstdlib>
#include <iostream>

using namespace std;

int mult( int x, int y, int randMax );

int main()
{
    int x = 0, 
        y = 0, 
        randMax;
    cout <<"Please enter a number between 2 and 21, this will determine how difficult your problems are.";
    cin >> randMax;
    cin.ignore();
    cout << "The product of your numbers is:" << mult ( x, y, randMax ) <<"\n";
    cin.get();
}

int mult (int x, int y, int randMax)
{
    x = rand() % randMax;
    y = rand() % randMax;
    return  x * y;
}

关于c++ - 我正在尝试创建一个程序,该程序根据用户输入的最大值来乘以随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17794961/

相关文章:

java - 数组中间随机枢轴的快速排序算法

c++ - WriteFile 返回错误代码 995

c++ - QT GUI 水平 slider setValue 与 QElapsedTimer

c - C 中 0 和 1 [0, 1) 之间的归一化随机数分布

java - 使用随机长整型生成随机 double

java - 有偏随机数生成器 - Java

python - 从 0/1 网格返回值

c++ - 错误 : ambiguous overload for 'operator=' in swap function using the copy-and-swap idiom

c++ - u8、u 和 U 字符串文字背后的推理

c++项目设计-私有(private)继承和 "is a"关系