Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Program that return random number

Posted on 2004-04-13
15
Medium Priority
?
476 Views
Last Modified: 2010-04-17
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
Comment
Question by:mocca23
  • 7
  • 7
15 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 10819725
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

by:mocca23
ID: 10827069
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
 
LVL 85

Expert Comment

by:ozo
ID: 10829184
What/where is GetRandomNum.h?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mocca23
ID: 10829495
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 85

Expert Comment

by:ozo
ID: 10829692
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

by:mocca23
ID: 10837714
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
 
LVL 85

Expert Comment

by:ozo
ID: 10837815
What errors are you seeing now?
0
 

Author Comment

by:mocca23
ID: 10837933
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 85

Expert Comment

by:ozo
ID: 10837976
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

by:mocca23
ID: 10838017
I  changed, but there is still the same error that I wrote above.
0
 
LVL 85

Expert Comment

by:ozo
ID: 10838055
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

by:mocca23
ID: 10838405
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 85

Accepted Solution

by:
ozo earned 110 total points
ID: 10839324
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
 

Author Comment

by:mocca23
ID: 10847709
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

by:proteus_82
ID: 10872792
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question