I've written the following code to generate random int or double numbers between given limits. When I run a build I get the error message "expression preceding parentheses of apparent call must have (pointer-to-) function type" for line 54. Does anyone know what I need to fix?
/*
* Program chapter 6_6
*
* This program generates and prints ten random integers
* Between user specified limits.
*/
#include <cstdlib> // required for srand(), rand().
#include <iostream> // required for cin, cout.
usingnamespace std;
// Function prototype.
int rand_int(int a, int b);
int main() {
// Declare objects.
int seed;
char random_type, I, F;
// Get seed value and interval limits.
cout << "Enter a positive integer seed value: \n";
cin >> seed;
// Ask user "Do you want to generate integers or floats, enter I or F"
cout << "Do you want to generate integers (I) or floats (F)? Enter I or F: \n";
cin >> random_type;
// IF statement to check random_type
if (random_type == I) {
// Seed the random number generator.
unsignedint seed;
int a, b;
srand(seed);
cout << "Enter integer limits a and b (a<b): \n";
cin >> a >> b;
// Generate and print ten random numbers.
cout << "Random Numbers: \n";
for (int k = 1; k <= 10; ++k) {
cout << rand_int(a, b) << ' ';
}
cout << endl;
}
elseif (random_type == F) {
// Ask user for float limits and accepts values for new float variable f1 and f2
float rand_float, a, b;
srand(seed);
cout << "Enter float limits f1 and f2: \n";
cin >> a >> b;
cout << "Random Numbers: \n";
for (double k = 1; k <= 10; ++k) {
cout << rand_float(a, b) << " ";
}
cout << endl;
}
// Exit program.
return 0;
}
/* This function generates a random integer
* between specified limits a and b (a<b).
*/
int rand_int(int a, int b) {
return rand() % (b - a + 1) + a;
}
double rand_float(double a, double b) {
return ((double)rand() / RAND_MAX) * (b - a) + a;
}
You have a variable and a function named rand_float() you can only have one identifier with the same name. Also you probably need a prototype for your function before you try to use it.