This question already has an answer here:
I am attempting to write a RNG code for class in which you input a minimum and maximum value, and the computer outputs a random number in between those values. The part that has me caught is you also have to input whether you want the generator to spit out a continuous or discreet value. If you input d, the RNG outputs an integer and the min and max values are inclusive. If you input c, the output is supposed to include decimals and it doesn't need to be inclusive. This is my code so far and the compiler error message. I am working in geany.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
double v1, v2, r2;
int r1;
char R;
cout << "What is the minimum value?" << endl;
cin >> v1;
cout << "What is the maximum value?" << endl;
cin >> v2;
r1=rand()%(v2-v1)+v1;
r2=((rand()%(RAND_MAX))/RAND_MAX)*(v2-v1)+v1;
cout << "Do you want a (d)iscrete or (c)ontinuous range?" << endl;
cin >> R;
if (R=='d') {cout << r1;}
if (R=='c') {cout << r2;}
return 0;
}
The compiler error is "error: invalid operands of types 'int' and 'double' to binary 'operator%'"
Any and all help is appreciated. Thanks!
Aucun commentaire:
Enregistrer un commentaire