我还不太擅长这个,我正在尝试学习如何让用户声明的变量在我的方程式中起作用。 现在,我只希望计算机根据用户指定的最大数字进行随机乘法运算。
当我尝试运行它时,机器会吐出这些错误:
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;
}
最佳答案
这里有不少错误。我会尽量友善。
- 您的两个
rand()
调用后都需要分号。 x
和y
没有在main()
的任何地方声明。我不知道您为什么要将它们作为参数传递给mult()
,但我认为 future 会有一些相关的功能。RAND_MAX
是一个常量,所以cin >> RAND_MAX
没有意义。相反,请参阅@Bill 的回答。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/