Reputation: 13
I'm getting the error message "no viable overloaded '='". This is the code as I have it now
#include <iostream>
#include <cmath>
using namespace std;
int main() {
auto n=0;
int p=0;
cout << "enter number n:"<< endl;
cin >> n ;
cout << p=pow(2,n)*n! << endl; //this is where I get the error message
cout << "the product is:" << endl;
cout << p << endl;
return 0;
}
Can anyone tell me what I have wrong?
Upvotes: 1
Views: 1012
Reputation: 40140
According to C++ Operator Precedence, operator <<
has precedence over operator =
.
This means
cout << p=pow(2,n)*n! << endl;
is read as
(cout << p)=(pow(2,n)*n! << endl);
which has no sense. Protect you assignment with parenthesis:
cout << (p=pow(2,n)*n!) << endl;
or better yet, split it in two statements:
p=pow(2,n)*factorial(n); // n! does not exist in C++.
cout << p << endl;
Upvotes: 8