Reputation: 29
can someone write the whole code modified for me please? i modify my own but still there is error , i want to use function and call it into printing main #include #include #include using namespace std;
int addition(int a=(rand()%99),int b=(rand()%99) {
int r;
r=a+b;
return r;
}
int main() {
int print, a, b;
print = addition(a, b);
cout << "this is result of addition" << print;
}
Upvotes: 2
Views: 2054
Reputation: 5126
There are two problems here:
First, you do not call srand
, which is necessary to set up a random seed (normally via srand(time(nullptr)
).
Secondly, the line print= int addition(int a,int b);
is written incorrectly. Assuming you want to use the default arguments, it should be print = addition();
, as you are not declaring the function, but calling it.
This would make the main function like this:
int main() {
srand(time(nullptr));
int print;
print= int addition();
cout<<"this is result of addition"<<print;
return 0;
}
Edit: This is assuming you actually want the behaviour that your function has default arguments which are random. This in itself seems like a bad design decision, and it would probably be better to send random numbers generated by the caller. This would make the resulting function:
int main() {
srand(time(nullptr));
int a = rand()%99, b = rand()%99;
int print;
print= int addition(a, b);
cout<<"this is result of addition"<<print;
return 0;
}
Also note that rand()%99 will return a value in the range [0,98].
Upvotes: 3
Reputation: 21528
You have some syntax error.. your main change in:
int print, a, b;
std::srand(std::time(0));
print = addition(a, b);
cout << "this is result of addition" << print;
anyway you can remove the declaration of 'a' and 'b' variables, since you does not use them in the code. In this way, you have to call addition without any parameters:
print = addition();
Upvotes: 0
Reputation: 171167
This line:
print = int addition(int a, int b);
is nonsense. You probably wanted:
print = addition();
Upvotes: 0