Help

#include <iostream>//this will include the "std::cout" stream for output to console.
#include <cmath>
#include <iomanip>
using namespace std; //this will allow you to avoid writing "std::" all the time.
int x;
int y;
char ch;
int mystery(int x, double y, char ch)
{


if (x == 0 && ch = 'A')
return (static_cast<int>(pow(y, 2)) + static_cast<int>(ch));
else if (x > 0)
return(x + static_cast<int>(sqrt(y)) + static_cast<int>(ch));
else
return(2 * x + static_cast<int>(y)-static_cast<int>(ch));
}
int main()
{
cout << mystery(0, 6.5, 'K') << endl;
cout << mystery(4, 16.0, '#') << endl;
cout << 2 * mystery(-11, 13.8, '8') << endl;
return 0;
}
When I run it the error C2106: "=":left operand must be l-value
and it is because of the first x after the if
Hi,

Welcome to cplusplus :+)

First up please always use code tags - use the <> button on the format menu.

using namespace std; //this will allow you to avoid writing "std::" all the time.

Actually a bad idea - in brings in the entire std namespace which is huge, and this will lead to naming conflicts - which defeats the whole purpose of namespaces !!
So stick with doing std::cout etc

1
2
3
int x;
int y;
char ch;


Don't use global variables, these look as they belong inside main Seen as you have hard coded actual numbers - they aren't necessary at all.

For any variables that you declare, always initialise them with something (on the same line)- even if you get input for them straight away. This is one of the biggest problems especially for beginners.

if (x == 0 && ch = 'A')

== is for comparison
= is for assignment

You have static_cast<int>(ch) several times in your code, so why not work that out once, then just use the variable you put the answer in?

Always use braces for conditional or loop statements - even if there is only 1 line - this will save you one day when you add more code.

Hope this helps
Topic archived. No new replies allowed.