Program that return random number

Hi, there is an error in this program, and I can not see it. Please help me.
Program should set up an overloaded function set called GetRandomNum. This function return a random number that is generated with srand() and rand(). There should be three different prototypes for this function:

int GetRandomNum(int m);
int GetRandomNum(int i, int j);
double GetRandomNum();

Program should make three calles to GetRandomNum: once otaining a number between 0 and 27, once otaining a number between 3 and 73, and last obtaining a number between 0.000 and 1.000. All three numbers should be displayed to the screen.

#include <iostream.h>
#include <stdlib.h>
#include "GetRandomNum.h"

int GetRandomNum(int m);
int GetRandomNum(int i, int j);
double GetRandomNum();

int main(void)
{
      int rnum;
      double dnum;
      srand(980);
      cout << "\n Enter random number between 0 and 27 \n\n";
      cout << "\n The random number is << rnum;
      GetRandomNum(27);
      cout << "\n Enter random number between 3 and 73 \n\n";
      GetRandomNum(3,73);
      cout << "\n Enter random number between 0.000 and 1.000 \n\n";
      GetRandomNum(0.0,1.0);
      return 0;
}


int GetRandomNum(int)
{
      int rnum;
      rnum=rand() % m;
      return rnum;
}


int GetRandomNum(int, int)
{
      int rnum;
      rnum=rand() % (j-i-1);
      rand=rand() % (i+1);
      return rnum;
}

double GetRandomNum()
{
      double dnum;
      cout.precision(3);
      cout.flags(ios::fixed | ios::showpoint);
      dnum=rand() % 1000/1000;
      return dnum;
}

Thanks,
mocca23
mocca23Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
GetRandomNum(int x, int y); takes two integer arguments, but
GetRandomNum(0.0,1.0); tries to pass it two float arguments

you declare    
  int rnum;
  double dnum;
in your main(void)
but you never do anything with them.
(the rnum and dnum declared in your GetRandomNum() functions are different local variables_

You never do anything with the return values from GetRandomNum.
Perhaps you meant to assign them to the rnum and dnum variables in your main
You can assign the result of a function like this:
  rnum = GetRandomNum(27);
then you probably want to send the number to your output, which you can do like
  cout << rnum;
0
 
ozoCommented:
20953174.cpp:5:14: warning: multi-line string literals are deprecated

20953174.cpp:20: warning: passing `double' for argument 1 of `int
   GetRandomNum(int, int)'
20953174.cpp:20: warning: passing `double' for argument 2 of `int
   GetRandomNum(int, int)'

20953174.cpp: In function `int GetRandomNum(int)':
20953174.cpp:28: `m' undeclared (first use this function)
20953174.cpp: In function `int GetRandomNum(int, int)':
20953174.cpp:36: `j' undeclared (first use this function)
20953174.cpp:36: `i' undeclared (first use this function)

Also, you never  display the number
0
 
mocca23Author Commented:
I made some changes, but there is still an error in this program.

#include <iostream.h>
#include <stdlib.h>
#include "GetRandomNum.h"

int GetRandomNum(int);
int GetRandomNum(int, int);
double GetRandomNum();

int main(void)
{
      int rnum;
      double dnum;
      srand(980);
      cout << "\n Enter random number between 0 and 27 \n\n";
      cout << "\n The random number is << rnum  \n";
      GetRandomNum(27);
      cout << "\n Enter random number between 3 and 73 \n\n";
      GetRandomNum(3,73);
      cout << "\n Enter random number between 0.000 and 1.000 \n\n";
      GetRandomNum(0.0,1.0);
      return 0;
}


int GetRandomNum(int)
{
      int rnum;
      rnum = rand() % x;
      return rnum;
}


int GetRandomNum(int, int)
{
      int rnum;
      rnum = rand() % (y - x - 1);
      rand = rand() % (x+1);
      return rnum;
}

double GetRandomNum()
{
      double dnum;
      cout.precision(3);
      cout.flags(ios::fixed | ios::showpoint);
      dnum=rand() % 1000/1000;
      return dnum;
}

Compiling...
ch08p19.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\ch08p19\ch08p19.cpp(3) : fatal error C1083: Cannot open include file: 'GetRandomNum.h': No such file or directory
Error executing cl.exe.

ch08p19.exe - 1 error(s), 0 warning(s)

Please help me!
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ozoCommented:
What/where is GetRandomNum.h?
0
 
mocca23Author Commented:
ozo,
I am lost now. Is it possible to use GetRandomNum.h like I did? How can I make it to work? I have been working on this program for two weeks. Please can you give me some clue to make the program to work.

I am beginner in C++.
0
 
ozoCommented:
GetRandomNum.h is not a standard c module.   It's probably something you need to write yourself.
But whatever it does you could just as easily do in your ch08p19.cpp file, so you can probably just omit the #include line and deal with the other errors in your program.
0
 
mocca23Author Commented:
I still don't know how to do. When I delete GetRandomNum.h, it finds more errors. Please help me! I am tired of looking in the program. Anything I change, there is an error.
Please, please help me.
mocca23
0
 
ozoCommented:
What errors are you seeing now?
0
 
mocca23Author Commented:
There are 1 error and 4 warnings.

 error C2659: '=' : overloaded function as left operand

I have to finish this program by Monday, so please help me (I said this thousand times).


 

0
 
ozoCommented:
int GetRandomNum(int)
{
     int rnum;
     rnum = rand() % x;
     return rnum;
}
//it looks like you are expecting x to be passed to the function.
//if so, you must declare it:
int GetRandomNum(int x)
0
 
mocca23Author Commented:
I  changed, but there is still the same error that I wrote above.
0
 
ozoCommented:
Does your compiler tell you which source line has that error?
If I had to guess, I'd say it was probably from
   rand = rand() % (x+1);
which seems to have no purpose
0
 
mocca23Author Commented:
I deleted  "rand = rand() % (x+1);". Now the compiler does not show any error but shows 4 warnings.

 warning C4244: 'argument' : conversion from 'const double' to 'int', possible loss of data
warning C4244: 'argument' : conversion from 'const double' to 'int', possible loss of data
warning C4101: 'rnum' : unreferenced local variable
warning C4101: 'dnum' : unreferenced local variable

Also it does not show the output. It is weird.

#include <iostream.h>
#include <stdlib.h>

int GetRandomNum(int x);
int GetRandomNum(int x, int y);
double GetRandomNum();

int main(void)
{
      
int rnum;
double dnum;
srand(980);
cout << "\n Random number between 0 and 27 \n\n";
cout << "\n The random number is << rnum  \n";
GetRandomNum(27);
cout << "\n Random number between 3 and 73 \n\n";
GetRandomNum(3,73);
cout << "\n Random number between 0.000 and 1.000 \n\n";
GetRandomNum(0.0,1.0);
return 0;
}


int GetRandomNum(int x)
{
int rnum;
rnum = rand() % x;
return rnum;
}


int GetRandomNum(int x, int y)
{
int rnum;
rnum = rand() % (y - x - 1);      
return rnum;
}

double GetRandomNum()
{
double dnum;
cout.precision(3);
cout.flags(ios::fixed | ios::showpoint);
dnum=rand() % 1000/1000;
return dnum;
}
0
 
mocca23Author Commented:
Now I have only one problem. The last function does not do the right job. It is supposed to do display any numbers between 0 and 1. Also, the output number should contain only three decimal places. I think something is not right with line 4. My output is now either 0 or 1.

double GetRandomNum()
{
      double dnum;
      dnum = (double) rand() / (double)1000; //this line
      cout.precision(3);
      return dnum;
}
Thanks,
mocca23
0
 
proteus_82Commented:
I belive that unless you output in the function call you have to set the precision where your outputting the number.  Otherwise you lose scope.
~proteus_82
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.