user2570222
user2570222

Reputation: 1

I am trying to create a program that multiplies random numbers based on a maximum the user inputs

Im not very good at this yet and I'm trying to learn how to get user declared variables to work within my equations. For now I just want the computer to spit out a randomized multiplication based on a maximum number specified by the user.

When I try to run this the machine spits back these errors:

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

The eventual goal is that the computer will generate the problem within the difficulty parameter then remove one of the variables in the equation to quiz the user.

#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;
}

Upvotes: 0

Views: 178

Answers (2)

bames53
bames53

Reputation: 88195

Others have pointed out problems such as trying to modify RAND_MAX, expecting that to change how rand() operates. I just want to show how to use the modern <random> library in place of rand().

There are a number of reasons not to use rand().

The reason which is most significant to your case is that it's not straightforward to use it to correctly get values in the range you want. The most common way people do it is something like rand() % randMax + 1, but for most values of randMax this will actually produce some numbers in the range [1,randMax] more often than other numbers. If it's important to get evenly distributed numbers then you need something more like:

int v;
do {
  v = rand();
} while (v >= RAND_MAX / randMax * randMax);
v = v % randMax + 1;

which isn't so simple. <random> provides many pre-made distributions so you often don't have to write your own like this.

Other reasons not to use rand() are that it's not thread safe or easy to use in a multithreaded program, and that usually it's not very random. <random> can be used to fix these problems as well.

Here's a version of your program using <random>.

#include <random>
#include <iostream>

// global random number engine and distribution to use in place of rand()
std::default_random_engine engine;
std::uniform_int_distribution<> distribution;

int mult()
{
    int x = distribution(engine);
    int y = distribution(engine);
    return  x * y;
}

int main()
{
    std::cout << "Please enter a number between 2 and 21, this will determine how difficult your problems are.";

    int max;
    std::cin >> max;

    // set the global distribution based on max
    distribution.param(std::uniform_int_distribution<>(1,max).param());

    std::cout << "The product of your numbers is:" << mult() << "\n";
}

Upvotes: 0

Pat Lillis
Pat Lillis

Reputation: 1180

There are quite a few errors here. I'll try and be kind.

  1. You need semi-colons after both of your rand() calls.
  2. x and y are not declared anywhere in main(). I don't know why you're passing them as parameters to mult(), but I assume there will be some related functionality down the line.
  3. RAND_MAX is a constant, so cin >> RAND_MAX makes no sense. Instead, see @Bill's answer.
  4. You need parens after cin.get.

Here's a working example, hopefully this is what you want it to do:

#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;
}

Upvotes: 1

Related Questions