The function Generate_And_Display_Bill() has no formal parameters and returns a variable of type float. The function generates a random number between $0.01 and $99.99 and returns the value to main().

This is what I've created so far in my function declaration;

#include <iostream>
#include <ctime>


float Generate_And_Display_Bill()
{

	float Random_Bill;

	srand(time(NULL));

	for(float index=0.01; index < 99.99; index++)
	{
		Random_Bill = (rand()%99.99)+1;
		cout << Random_Bill << endl; 
	}

	return (Random_Bill);
}

I'm receiving an error telling me that in line 14 the 99.99 must be have integral or enum type, I want to display a random bill with decimals and I've declared Random_Bill as a float, what am I doing wrong?

Also for some odd reason "cout" and "endl" are giving me unidentified error messages even though I have included the iostream library, please help

Recommended Answers

All 10 Replies

for loops require integers as the increment value. Why are you trying to loop through all the valid values? The requirements are to generate one (1) number and return it.

>> Also for some odd reason "cout" and "endl" are giving me unidentified error messages even though I have included the iostream library, please help

did you forget using namespace std; ?

Thanks @Momerath yeah I don't know why I was trying to create several random numbers I've fixed it so it creates only one random number
And @Nick Evan yes I completely forgot using namespace rookie mistake cout and endl are now recognizable

Here is my revision still getting errors with 99.99 though;

#include <iostream>
#include <ctime>
 
using namespace std;

float Generate_And_Display_Bill()
{

	float Random_Bill;

	srand(time(NULL));

	Random_Bill=(float)((rand()%99.99)*0.01);

	cout << Random_Bill; //Test to see value

	return (Random_Bill);
}

The modulus operator is an integer operation. It returns to you the remainder after integer division. Dividing by a floating point number gives you an exact floating point value (no remainder) so the operation makes no sense. If you want a random floating point value in a particular range I'd suggest you look into the drand48 function as a replacement to the rand call in your code.

How does drand48() work? is drand48() within the ctime library? If I use it will I have to change line 11 in my code as well?

*I've checked my textbook and I can't find anything on that function

Or you could pick a random number between 0-1000 and divide it by 1000?

Okay instead of making this more complicated than it needs to be I decided to use rand())/RAND_MAX*100 to produce a float between 0 and 99

float Generate_And_Display_Bill()
{

	float Random_Bill;
	Random_Bill = 0;

	srand((unsigned)time(NULL));


	Random_Bill= (float(rand())/RAND_MAX*100);

	cout << Random_Bill; //Test to see value

	return (Random_Bill);
}

However, my ouput isn't really random at all and I want the output to the hundredths decimal space .00

Seed your random number generator (srand - or one of it's variants). And drand48 is not complicated and does a better job of what you want (no converting and/or modulus).

#include <iostream>
#include <cstdlib>

double use_drand () { return drand48 () * 100.0; }

int main () {
   for (int i = 0; i < 10; ++i)
      std::cout << use_drand () << std::endl;
   return 0;
}

[edit]
You'll want to seed drand48 with srand48.

Seed your random number generator (srand - or one of it's variants). And drand48 is not complicated and does a better job of what you want (no converting and/or modulus).

#include <iostream>
#include <cstdlib>

double use_drand () { return drand48 () * 100.0; }

int main () {
   for (int i = 0; i < 10; ++i)
      std::cout << use_drand () << std::endl;
   return 0;
}

Because I haven't learned about drand () in my programming class and also because I honestly don't understand how it works either, I stuck with the ol' rand () function and I messed around for a bit until I created pretty random bill generator;

float Generate_And_Display_Bill()
{

	float Random_Bill;
	Random_Bill = 0;

	srand((unsigned)time(NULL));

	while(!(Random_Bill >= .01 && Random_Bill <= 99.99))
	{
		Random_Bill = ((float)rand ())/100;
	}

	cout << Random_Bill; //Test to see value

	return (Random_Bill);
}

so far all my values come out to the hundredths decimal space and stay within the bill range

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.