I am learning C++ and one of the programs was for a random number generator.
Once I wrote the program I got the following errors:
dice.cpp: In function ‘int main()’:
dice.cpp:18: error: pointer to a function used in arithmetic
dice.cpp:18: error: invalid conversion from ‘int (*)(int)’ to ‘int’
Here is my code:
#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
int randn(int n);
int main()
{
int q;
int n;
int r;
srand(time(NULL));
cout<<"Enter a number of dice to roll: ";
cin>>n;
cout<<endl;
for (q=1; q<=n; q++)
{
r=randn+1; // <-- error here
cout<<r<<endl;
}
return 0;
}
int randn(int n)
{
return rand()%n;
}
What could be the problem?
I believe that your problem is this line:
I believe that you meant to write
The issue is that you’re trying to call the function but forgetting to put in the parentheses indicating that you’re making the call. Since you’re trying to roll a six-sided die, this should probably read
Hope this helps!