Solved

# Program that return random number

Posted on 2004-04-13
421 Views
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
0
Question by:mocca23
• 7
• 7

LVL 84

Expert Comment

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

Author Comment

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)

0

LVL 84

Expert Comment

What/where is GetRandomNum.h?
0

Author Comment

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

LVL 84

Expert Comment

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

Author Comment

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.
mocca23
0

LVL 84

Expert Comment

What errors are you seeing now?
0

Author Comment

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

LVL 84

Expert Comment

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

Author Comment

I  changed, but there is still the same error that I wrote above.
0

LVL 84

Expert Comment

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

Author Comment

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

LVL 84

Accepted Solution

ozo earned 55 total points
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;
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

Author Comment

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

Expert Comment

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

## Featured Post

### Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …